简体   繁体   English

如何使用变量来引用文本框?

[英]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.我正在尝试使用变量来引用文本框,例如 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'.这也不起作用,每次说“tempBox is Nothing”时都会吐出一个错误。

Can anyone tell me if I can make this work?谁能告诉我是否可以完成这项工作?

Thanks.谢谢。

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 .默认情况下,该控件将存在于其父级的Controls 属性中,并且由于您试图通过其名称获取该控件,因此您可以使用 ControlCollection 的Find 方法 If you can guarantee that the control's parent is the Form then you'd call:如果您可以保证控件的父级是 Form,那么您可以调用:

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:但是,如果控件的父级可能不是 Form,那么您可以调用:

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.第一个会执行得稍微快一些,因为它只迭代当前 ControlCollection 而第二个可能需要更长的时间,因为如果它在当前 ControlCollection 中找不到控件,那么它也会开始迭代子控件。

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.在 Mary 发表评论后,我编辑了我的答案以添加这一行 --> 如果 Option Strict 为 On 并且“For”从 0 或 1 或任何数字开始并且 txtKills[X] 存在,则我的代码不起作用。

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".你的代码工作正常,但我认为你有一个错误,因为你的 For 从 0 开始并且你没有任何“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

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.假设所有控件都在 Form 中作为父控件,并且它们都以 txtKills 开头...如果您要将这些文本框作为一组用于多个操作,您可能需要构建一个 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM