MCSGaming
MCSGaming

Reputation: 3

How can I use a variable to reference a textbox?

I'm new to visual basic and programming in general, but I'm trying to make a statistic counter sort of program. I'm trying to use a variable to reference a textbox, for example, k_kills(i) = txtKills(i).Text. This doesn't work, however, so I then tried the following:

For i = 0 To 8
            Dim tempBox As TextBox
            Dim tempName As String = "txtKills" & i.ToString
            tempBox = Me.Controls.Item(tempName)

            k_kills(i) = tempBox.Text
Next

This also doesn't work and spits out an error each time saying that 'tempBox was Nothing'.

Can anyone tell me if I can make this work?

Thanks.

Upvotes: 0

Views: 2326

Answers (3)

UnnamedString
UnnamedString

Reputation: 11

After Mary's comment I edit my answer to add this line --> My code does not work if Option Strict is On and 'For' starting in 0 or 1 or any number and txtKills[X] exists.

This was my previous answer and I don't know if I have to delete or not:

Your code works fine but I think you have an error because your For starts in 0 and you don't have any "txtKills0". I've tested it now:

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim k_kills(10) As String '<< Ignore the length
        For i = 1 To 7
            Dim tempBox As TextBox
            Dim tempName As String = "txtKills" & i.ToString
            tempBox = Me.Controls.Item(tempName)

            k_kills(i) = tempBox.Text
            MsgBox(k_kills(i))
        Next

    End Sub

Upvotes: 0

Mary
Mary

Reputation: 15101

Assuming the controls are all in Form as parent and they all start with txtKills... If you are going to use these text boxes as a group for several actions you may want to build an array or list of TextBox.

Dim Kills(7) As TextBox

Private Sub CreateTextBoxArray()
   Dim index As Integer
   For Each ctrl As Control In Controls
        If ctrl.Name.StartsWith("txtKills") Then
            Kills(index) = DirectCast(ctrl, TextBox)
            index += 1
        End If
    Next
End Sub

Private Sub ClearKillTextBoxes()
    For Each t In Kills
        t.Clear()
    Next
End Sub

Private Function GetTextFromKillBoxes() As List(Of String)
    Dim lst As New List(Of String)
    For Each t In Kills
        lst.Add(t.Text)
    Next
    Return lst
End Function

Upvotes: 0

David
David

Reputation: 6131

You will need to find the control in some collection. By default the control would exist in its parent's Controls property and since you're trying to get the control by its name then you could use ControlCollection's Find method. If you can guarantee that the control's parent is the Form then you'd call:

Dim tempBox As TextBox = DirectCast(Me.Controls.Find(tempName, False), TextBox)

But if there is the possibility that the control's parent is something other than the Form then you'd call:

Dim tempBox As TextBox = DirectCast(Me.Controls.Find(tempName, True), TextBox)

The first would execute slightly quicker because it only iterates over the current ControlCollection whereas the second could take longer because if it cannot find the control in the current ControlCollection then it starts to iterate over the child controls as well.

Upvotes: 2

Related Questions