|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Newbie question - VB.net, referencing controlsHoping 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 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 > 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
Show quote
Hide quote
"jkbat" <jkba***@gmail.com> schrieb: Accessing controls by their names or indices> 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 <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/> 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/> 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/> > 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/> > >
RegEx Either Or
GPS & Windows Mobile XML into SQL 2005 Express using VB 2005 Discrepancy in DataGridView column order & databound DataTable Icon.FromHandle(..) not working? Lauch VB.NET App From Network Drive Or Network Share. Need help/advice to make a decision positioning a watermark on a web page regardless of screen size Any way to pass an optional array? Move from one cell to the next cell in a datagrid |
|||||||||||||||||||||||