Results 1 to 11 of 11

Thread: Addressing a series of textboxes in a loop?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    140

    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

  2. #2
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    140

    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"
    ?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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

  5. #5
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    140

    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

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Addressing a series of textboxes in a loop?

    Quote Originally Posted by dday9 View Post
    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.

  10. #10
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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.

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Addressing a series of textboxes in a loop?

    Quote Originally Posted by Shaggy Hiker View Post
    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
  •  



Click Here to Expand Forum to Full Width