Home All Groups Group Topic Archive Search About

Newbie question - VB.net, referencing controls

Author
25 Jul 2006 5:54 PM
jkbat
Hi,

Hoping this isn't too trivial a question:

I have a form with TextBoxes named TextBox1, TextBox2, etc. I'd like to
iterate through a whole series of these and set their values based
(initially) on a counter. I'm thinking I need a way to convert a string
to a control's reference?

What I want to do in (pseudo-ish) code is:

Dim str as String
Dim i as Integer

Do
     Me.TextBox(i).Text = i
Loop Until i = 32

I can generate the appropriate string variable with the value I need
i.e.
str = "Me.TextBox" & i & ".Text"

but it's not being picked up as a control reference. I've had a look at
DirectCast, but can't seem to figure it out...

Any ideas?

Cheers,

Jimmy

Author
25 Jul 2006 6:02 PM
Cor Ligthert [MVP]
JKbat,

Herfried will show for sure a link, but if the textboxes are in a panel let
say panel1, than you can do

dim i as integer = 1
for each txtb as control in panel1
    txtb.text = i.tostring
    i += 1
next

On this are endless variations, but because that Herfried will show that
link anyhow I keep it with this,

I hope this helps,

Cor


Show quoteHide quote
"jkbat" <jkba***@gmail.com> schreef in bericht
news:1153850056.494122.6950@m73g2000cwd.googlegroups.com...
> Hi,
>
> Hoping this isn't too trivial a question:
>
> I have a form with TextBoxes named TextBox1, TextBox2, etc. I'd like to
> iterate through a whole series of these and set their values based
> (initially) on a counter. I'm thinking I need a way to convert a string
> to a control's reference?
>
> What I want to do in (pseudo-ish) code is:
>
> Dim str as String
> Dim i as Integer
>
> Do
>     Me.TextBox(i).Text = i
> Loop Until i = 32
>
> I can generate the appropriate string variable with the value I need
> i.e.
> str = "Me.TextBox" & i & ".Text"
>
> but it's not being picked up as a control reference. I've had a look at
> DirectCast, but can't seem to figure it out...
>
> Any ideas?
>
> Cheers,
>
> Jimmy
>
Author
25 Jul 2006 6:10 PM
iwdu15
well i dunno if you can reference a control by a variable name....id try it
this way

For Each txt As TextBox In Me.Controls

Select Case txt.Name

.....

End Select

        Next


hope that helps

--
-iwdu15
Author
25 Jul 2006 7:11 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"jkbat" <jkba***@gmail.com> schrieb:
> I have a form with TextBoxes named TextBox1, TextBox2, etc. I'd like to
> iterate through a whole series of these and set their values based
> (initially) on a counter. I'm thinking I need a way to convert a string
> to a control's reference?
>
> What I want to do in (pseudo-ish) code is:
>
> Dim str as String
> Dim i as Integer
>
> Do
>     Me.TextBox(i).Text = i
> Loop Until i = 32

Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
26 Jul 2006 10:27 AM
jkbat
Thanks to all for the input! I've used a bits from the suggestions
provided; here's what I've got to work:

        Dim str As String
        Dim ctr As Control
        Dim i As Integer = 1

        For Each ctr In Me.Controls
            If TypeOf ctr Is TextBox Then
                str = "TextBox" & i
                DirectCast(FindControl(str, Me), TextBox).Text = str
                i += 1
            End If
        Next

All the best,

Jimmy

Herfried K. Wagner [MVP] wrote:

Show quoteHide quote
> "jkbat" <jkba***@gmail.com> schrieb:
> > I have a form with TextBoxes named TextBox1, TextBox2, etc. I'd like to
> > iterate through a whole series of these and set their values based
> > (initially) on a counter. I'm thinking I need a way to convert a string
> > to a control's reference?
> >
> > What I want to do in (pseudo-ish) code is:
> >
> > Dim str as String
> > Dim i as Integer
> >
> > Do
> >     Me.TextBox(i).Text = i
> > Loop Until i = 32
>
> Accessing controls by their names or indices
> <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
Author
26 Jul 2006 10:52 AM
Cor Ligthert [MVP]
Jimmy,

The DirectCast to Textbox is not needed in this situation.
Text is a in textbox derived property from control and can therefore direct
be used.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.text.aspx

I thought it would be helpful to know.

Cor



Show quoteHide quote
"jkbat" <jkba***@gmail.com> schreef in bericht
news:1153909638.081522.326140@m73g2000cwd.googlegroups.com...
> Thanks to all for the input! I've used a bits from the suggestions
> provided; here's what I've got to work:
>
>        Dim str As String
>        Dim ctr As Control
>        Dim i As Integer = 1
>
>        For Each ctr In Me.Controls
>            If TypeOf ctr Is TextBox Then
>                str = "TextBox" & i
>                DirectCast(FindControl(str, Me), TextBox).Text = str
>                i += 1
>            End If
>        Next
>
> All the best,
>
> Jimmy
>
> Herfried K. Wagner [MVP] wrote:
>
>> "jkbat" <jkba***@gmail.com> schrieb:
>> > I have a form with TextBoxes named TextBox1, TextBox2, etc. I'd like to
>> > iterate through a whole series of these and set their values based
>> > (initially) on a counter. I'm thinking I need a way to convert a string
>> > to a control's reference?
>> >
>> > What I want to do in (pseudo-ish) code is:
>> >
>> > Dim str as String
>> > Dim i as Integer
>> >
>> > Do
>> >     Me.TextBox(i).Text = i
>> > Loop Until i = 32
>>
>> Accessing controls by their names or indices
>> <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
>>
>> --
>>  M S   Herfried K. Wagner
>> M V P  <URL:http://dotnet.mvps.org/>
>>  V B   <URL:http://classicvb.org/petition/>
>
Author
26 Jul 2006 11:16 AM
jkbat
Cor,

Thanks for that.

FindControl(str, Me).Text = str

does indeed work.

Cheers,

Jimmy
Cor Ligthert [MVP] wrote:

Show quoteHide quote
> Jimmy,
>
> The DirectCast to Textbox is not needed in this situation.
> Text is a in textbox derived property from control and can therefore direct
> be used.
>
> http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.text.aspx
>
> I thought it would be helpful to know.
>
> Cor
>
>
>
> "jkbat" <jkba***@gmail.com> schreef in bericht
> news:1153909638.081522.326140@m73g2000cwd.googlegroups.com...
> > Thanks to all for the input! I've used a bits from the suggestions
> > provided; here's what I've got to work:
> >
> >        Dim str As String
> >        Dim ctr As Control
> >        Dim i As Integer = 1
> >
> >        For Each ctr In Me.Controls
> >            If TypeOf ctr Is TextBox Then
> >                str = "TextBox" & i
> >                DirectCast(FindControl(str, Me), TextBox).Text = str
> >                i += 1
> >            End If
> >        Next
> >
> > All the best,
> >
> > Jimmy
> >
> > Herfried K. Wagner [MVP] wrote:
> >
> >> "jkbat" <jkba***@gmail.com> schrieb:
> >> > I have a form with TextBoxes named TextBox1, TextBox2, etc. I'd like to
> >> > iterate through a whole series of these and set their values based
> >> > (initially) on a counter. I'm thinking I need a way to convert a string
> >> > to a control's reference?
> >> >
> >> > What I want to do in (pseudo-ish) code is:
> >> >
> >> > Dim str as String
> >> > Dim i as Integer
> >> >
> >> > Do
> >> >     Me.TextBox(i).Text = i
> >> > Loop Until i = 32
> >>
> >> Accessing controls by their names or indices
> >> <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
> >>
> >> --
> >>  M S   Herfried K. Wagner
> >> M V P  <URL:http://dotnet.mvps.org/>
> >>  V B   <URL:http://classicvb.org/petition/>
> >