mosh.jinton
mosh.jinton

Reputation: 328

How to use a variable in referencing the name of a control

I'm currently learning how to use Visual Basic as part of an AS-Level course. For a project I'm working on, I'd like to create a loop whereby the contents of a text box are entered into something else, not relevant to this question. The text boxes are labelled txtName1, txtName2, and so on, so I figured the easiest way to do this would be something like

Do Until r = 10
    placeholder = txtName(r).Text
    r = r + 1
Loop

But this doesn't seem to work. Is there any way to use a variable in an object reference? Thank you in advance.

Upvotes: 3

Views: 702

Answers (5)

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

Assuming Winforms rather than Webforms, WPF, or MVC, my preferred solution to this problem is to place each of your textboxes in a common parent control. This could be a groupbox, a panel, or even the form itself. Then you have code like this:

For Each box As TextBox In myParent.Controls.OfType(Of TextBox)()
    placeholder = box.Text
Next box

Upvotes: 3

Victor Zakharov
Victor Zakharov

Reputation: 26414

If you need to reference controls like that, chances are you need to look into changing your design.

For example, you could have created a user control MyTextBoxContainer, where these TextBoxes are created dynamically (they all share a similar purpose, right?), and then provide an accessor property TextAtIndex, which takes an index and returns Text property of specified TextBox as a String. When adding to controls collection of your user control, you would also keep references to these controls in a Generic.List for internal use (so that indexing could work).

Upvotes: 1

ElektroStudios
ElektroStudios

Reputation: 20464

You could also first store the controls that you would need to touch in a container (a List type or Dictionary type), and then, take those that you need from the variable, avoiding this way iterating all the time the UI controls on each search.

PS: You could use a dictionary(of integer, tetxbox) using keys as an index to have more flexibility on them.

Public Class Form1

    Private TextBoxes As List(Of TextBox)
    Private placeholder As String

    Private Shadows Sub Load() Handles MyBase.Load
        TextBoxes = Me.Controls.OfType(Of TextBox)().Reverse.ToList
    End Sub

    Private Shadows Sub Shown() Handles MyBase.Shown

        For Each TB As TextBox In TextBoxes.GetRange(0, 10) ' From txtName1 to txtName10
            placeholder = TB.Text
        Next

    End Sub

End Class

Another solution to do it without the container requeriment:

Private Sub Test()

    For Each TB As TextBox In Me.Controls.
                              OfType(Of TextBox).
                              Reverse.
                              TakeWhile(Function(x) x.Name.
                                                      Split("txtName".ToCharArray).
                                                      Last <= 10)

        MsgBox(TB.Name) ' From txtName1 to txtName10

    Next

End Sub

Upvotes: 1

Markus
Markus

Reputation: 22436

In a Windows Forms project, you can use the Find-method on the Controls-collection to find a control by name. This also works for controls that are located deeper in the tree (e.g. if a control is on a Panel):

For i As Integer = 1 To 10
    placeholder = Controls.Find("txtName" & i, True).First().Text
Next

Upvotes: 2

Satpal
Satpal

Reputation: 133403

You can use FindControl

Try

  placeholder = ((TextBox)Me.FindControl(txtName & cstr(r))).Text

Upvotes: 3

Related Questions