-
Jul 28th, 2014, 01:36 PM
#1
Thread Starter
Addicted Member
Addressing a series of textboxes in a loop?
Hi again!
Having defined a series of textboxes on a form which are logically related, I'd like to assign text to each one in a loop.... The textboxes are numbered TextBox1 through TextBox15, and I'd like to assign each one in turn from a string I'll be parsing out character by character.... Something like this:
Code:
Public letters As String = "ABCDEFGHIJKLMN"
for I = 0 to (letters.length) -1
Textbox(I+1).text = letters.substring(I,1)
next
With the hoped-for result being that TextBox1 contains an "A", TextBox2 contains a "B", and so on.....
Can anybody point me at a more-elegant way to accomplish this? Even a not-so-elegant way that will actually work?
Admittedly it COULD be done (I think) with something dumb like
Code:
Public letters As String = "ABCDEFGHIJKLMN"
for I = 0 to (letters.length) -1
if I == 0 then TextBox1.text = letters.substring(I,1)
if I == 1 then TextBox2.text = letters.substring(I,1)
if I == 2 then TextBox3.text = letters.substring(I,1)
... Etc....
next
But that's just Sooooo brute-force!
Suggestions, anybody?
Thanks for any help you gurus (& gurettes!) can offer!
JoeP
-
Jul 28th, 2014, 02:55 PM
#2
Fanatic Member
Re: Addressing a series of textboxes in a loop?
There are two ways to achieve this. One way is addressing the real control names:
Code:
Dim str As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
For i As Integer = 1 To 15 'Assuming having 15 TextBoxes
For Each c As Char In str
If c = CType(Me.Controls("Textbox" & i), TextBox).Text Then
MsgBox("Textbox" & i & " is " & c.ToString)
End If
Next
Next
Another way is addressing each textbox in fom:
Code:
Dim str As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
For Each Control As TextBox In Me.Controls.OfType(Of TextBox)() 'For each TextBox
For Each c As Char In str
If c = Control.Text Then
MsgBox(Control.Name & " is " & c.ToString)
End If
Next
Next
God, are you punishing me because my hair is better than yours? -Jack Donaghy
-
Jul 28th, 2014, 03:19 PM
#3
Thread Starter
Addicted Member
Re: Addressing a series of textboxes in a loop?
Thanks, Flashbond!
Close, but no cigar.... I want to write the values to the textboxes, not check the values in the textboxes.... I'm liking your first method better than the second, because I won't actually want to address ALL the textboxes..... Just 2-15... So, how would I go about ASSIGNING values to these puppies? Maybe something like
Code:
Me.Controls("Textbox" & I).text = "A"
?
-
Jul 28th, 2014, 03:23 PM
#4
Re: Addressing a series of textboxes in a loop?
Yes, almost exactly something like that, with one caveat: Every control is in the Controls collection of some container. That code assumes that all of the textboxes are all on the main form (Me). If they are in a groupbox, on a panel, on a tab, or in any other container, they will be in the Controls collection for the container, not the controls collection for the main form.
One other point is that Me.Controls("whatever") will always return an object of type Control. That should be fine for Text, but if you want to do more, you may have to cast it to the proper control:
DirectCast(Me.Controls("whatever"),Textbox).Text
Without the cast, you have a Control, and can only access the properties that are common to the Control class. To access properties specific to some class derived from control, you have to cast it to the appropriate type.
My usual boring signature: Nothing
-
Jul 28th, 2014, 03:32 PM
#5
Fanatic Member
Re: Addressing a series of textboxes in a loop?
Yeah, Shaggy Hiker is right. With his correction:
Code:
Dim str As String = "ABCDEFGHIJKLMN"
For i As Integer = 2 To 15
DirectCast(Me.Controls("Textbox" & i), TextBox).Text = str.Substring(i - 2, 1)
Next
God, are you punishing me because my hair is better than yours? -Jack Donaghy
-
Jul 28th, 2014, 03:32 PM
#6
Thread Starter
Addicted Member
Re: Addressing a series of textboxes in a loop?
Thanks, Shaggy! All the textboxes I'm needing to stuff stuff in are on the main form(Me.) or on form2, which will have the same textbox numbers as form1... the only other thing on the forms is a couple of pictureboxes that I'm using as "dividers", so nothing fancy.... I'll give it a try without casting anything and see how it goes...
Thanks again!!
Joep
-
Jul 28th, 2014, 03:44 PM
#7
Re: Addressing a series of textboxes in a loop?
The casting shouldn't be necessary if all you want to access is the .Text property, since that is a member of the control class.
My usual boring signature: Nothing
-
Jul 28th, 2014, 04:32 PM
#8
Re: Addressing a series of textboxes in a loop?
Personally the way that I would do it is by adding all the TextBoxes that you're wanting to assign the text to into a List(Of TextBox). Then rather then iterating through a control's Controls property, you'd iterate through the list. This way the collection you're iterating through is indeed a TextBox and not some other control, plus you get the added benefit of not having to cast the control because it's t value is a TextBox.
-
Jul 28th, 2014, 04:34 PM
#9
Re: Addressing a series of textboxes in a loop?
Originally Posted by dday9
Personally the way that I would do it is by adding all the TextBoxes that you're wanting to assign the text to into a List(Of TextBox). Then rather then iterating through a control's Controls property, you'd iterate through the list. This way the collection you're iterating through is indeed a TextBox and not some other control, plus you get the added benefit of not having to cast the control because it's t value is a TextBox.
Agreed other then the collection will not change so an array is more suited.
-
Jul 29th, 2014, 10:54 AM
#10
Re: Addressing a series of textboxes in a loop?
An array is a fixed size mass of mutable state. If the collection isn't changing, an array seems unsuitable as well.
-
Jul 29th, 2014, 12:47 PM
#11
Re: Addressing a series of textboxes in a loop?
Originally Posted by Shaggy Hiker
Yes, almost exactly something like that, with one caveat: Every control is in the Controls collection of some container. That code assumes that all of the textboxes are all on the main form (Me). If they are in a groupbox, on a panel, on a tab, or in any other container, they will be in the Controls collection for the container, not the controls collection for the main form.
One other point is that Me.Controls("whatever") will always return an object of type Control. That should be fine for Text, but if you want to do more, you may have to cast it to the proper control:
DirectCast(Me.Controls("whatever"),Textbox).Text
Without the cast, you have a Control, and can only access the properties that are common to the Control class. To access properties specific to some class derived from control, you have to cast it to the appropriate type.
They added a method to make it easier to find nested controls.
The controls collection of a container (so form, panel, etc..) has a find() method now that takes a control name string and a Boolean you can pass true in to search all child containers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|