循环遍历 vb.net 中的文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30604633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:10:42  来源:igfitidea点击:

Loop through textboxes in vb.net

vb.net

提问by Anel Hodzic

I remember in vb6 you were able to create the array of textboxes.

我记得在 vb6 中你可以创建文本框数组。

Textbox1(0), Textbox1(1) ..... ,

But in vb.net you can't create array ? So if you have code like this . Is it possible set it into for loop ?

但是在 vb.net 中你不能创建数组?所以如果你有这样的代码。是否可以将其设置为 for 循环?

        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
        TextBox9.Text = ""
        TextBox10.Text = ""
        TextBox11.Text = ""
        TextBox12.Text = ""
        TextBox13.Text = ""
        TextBox14.Text = ""
        TextBox15.Text = ""

回答by LarsTech

If the TextBox controls are just on the main form, then you can loop through them:

如果 TextBox 控件只是在主窗体上,那么您可以遍历它们:

For Each tb As TextBox In Me.Controls.OfType(Of TextBox)()
  tb.Text = String.Empty
Next

If they are in a panel, then replace the Mekeyword with the name of the panel.

如果它们在面板中,则将Me关键字替换为面板名称。

回答by jwatts1980

You can create a List and loop through it:

您可以创建一个 List 并循环遍历它:

Dim boxes As New List(Of TextBox)() From { _
    TextBox1, _
    TextBox2 _
}
boxes.Add(TextBox3)

For Each tb As TextBox In boxes
    tb.Text = ""
Next

If you have a Form with TextBox controls down inside other controls such as a Panel, or GroupBox, you can try to use a recursive function like this to get them all. (This is basically a C# to VB conversionof the answer here)

如果在其他控件(例如 Panel 或 GroupBox)内有一个带有 TextBox 控件的表单,则可以尝试使用这样的递归函数来获取所有这些控件。(这基本上是这里答案的C# 到 VB 转换

Private Function GetTextBoxes(root As Control) As IEnumberable(Of TextBox)
    Dim container = TryCast(root, ContainerControl)
    If container IsNot Nothing Then
        For Each c As Control In container.Controls
            For Each i As Control In GetTextBoxes(c)
                Yield i
            Next
        Next
    End If
End Function

To create a list from your main form:

要从主表单创建列表:

Dim allBoxes As List(Of TextBox) = GetTextBoxes(Me).ToList()

回答by RianBattle

For reference, you CAN create an Array of TextBox objects, in the following manner:

作为参考,您可以按以下方式创建一个 TextBox 对象数组:

Dim tbArray() As TextBox = New TextBox() {TextBox1, TextBox2, TextBox3}

Dim tbArray() As TextBox = New TextBox() {TextBox1, TextBox2, TextBox3}

Or, declare the array and loop through the TextBox controls to add them to it. However, the List(Of TextBox)approach will work just fine if you need to keep a collection of them, or simply looping through the TextBox controls on the form if you just need to set the properties in one sub or function.

或者,声明数组并循环遍历 TextBox 控件以将它们添加到其中。但是,List(Of TextBox)如果您需要保留它们的集合,或者如果您只需要在一个子或函数中设置属性,则只需在表单上的 TextBox 控件中循环,该方法就可以正常工作。