控制数组 vb.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14789112/
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 12:16:39  来源:igfitidea点击:

control array vb.net

vb.net

提问by Asjad Athick

Im trying to write a program in VB.net for a shopping system. It will read through the database and populate the items on the form. The app displays information such as product name etc in labels, inside a scrollable panel. Im creating the objects and assigning values such as text at runtime. I'm using a loop for the code.

我正在尝试在 VB.net 中为购物系统编写程序。它将通读数据库并填充表单上的项目。该应用程序在可滚动面板内的标签中显示产品名称等信息。我在运行时创建对象并分配诸如文本之类的值。我正在为代码使用循环。

If i was using vb 6, i would have a control array, and use the index and my counter to display the data. Since im doing this in vb.net, i have no way to do that.. Any solutions?

如果我使用 vb 6,我会有一个控制数组,并使用索引和我的计数器来显示数据。由于我在 vb.net 中这样做,我没有办法做到这一点.. 任何解决方案?

回答by Steven Doggart

VB.NET does not support control arrays, in the same sense as VB6. You can do similar things, though. For instance, if you want to handle events from multiple controls with the same method, you can do so like this:

VB.NET 不支持控件数组,与 VB6 的意义相同。不过,你可以做类似的事情。例如,如果您想使用相同的方法处理来自多个控件的事件,您可以这样做:

Private Sub MyClickHandler(sender As Object, e As EventArgs) Handles _
    Button1.Click, _
    Button2.Click, _
    Button3.Click

    Dim buttonThatWasClicked As Button = CType(sender, Button)
    ' Do something...
End Sub

If you want to create an array of controls that you can loop through, you can do that to, like this:

如果要创建可以循环遍历的控件数组,可以这样做,如下所示:

Dim myTextBoxes() As TextBox = New TextBox() { TextBox1, TextBox2, TextBox3 }
For i As Integer = 0 to myTextBoxes.Length - 1
    myTextBoxes(i).Text = ...
Next

Alternatively, if you name your controls consistently, you can find them by name in your form's Controlscollection. For instance, if you had three text boxes named TextBox1, TextBox2, and TextBox3, you could loop through them like this:

或者,如果您为控件命名一致,则可以在表单Controls集合中按名称找到它们。举例来说,如果你有三个文本框命名TextBox1TextBox2TextBox3,你可能会通过这些循环是这样的:

For i As Integer = 1 to 3
    Dim t As TextBox = CType(Me.Controls("TextBox" & i.ToString()), TextBox)
    t.Text = ...
Next

回答by Oded

VB.NET doesn't have control arrays as such.

VB.NET 本身没有控件数组。

However, you cancreate an array of controls and assign your controls to each element of the array, though you could also use a List(Of Control).

但是,您可以创建一个控件数组并将您的控件分配给该数组的每个元素,但您也可以使用List(Of Control).

This will allow you to loop over the collection.

这将允许您遍历集合。

回答by Abdusalam Ben Haj

There are no Control arrays in VB.NET . But you could iterate through Panel.Controlscollection. All the controls are in that collection (If they are all in the same panel).

VB.NET 中没有 Control 数组。但是您可以遍历Panel.Controls集合。所有控件都在该集合中(如果它们都在同一个面板中)。

    For i = 0 To Panel1.Controls.Count - 1

        Dim control = Panel1.Controls(i)

        'Do something with control..

    Next

回答by peterG

First is there a reason why you can't use a grid for this? - that would be the obvious solution (as it would have been in VB6 as well).

首先,为什么不能为此使用网格?- 这将是显而易见的解决方案(就像在 VB6 中一样)。

ETA . . .but if you must, this code snippet will add a set of labels to your form. You will need to modify this eg replace the for next loop with a for each r as mydataset.mytabledatarow in mydataset.mydatable etc etc

预计到达时间 . .但如果您必须这样做,此代码段将向您的表单添加一组标签。您将需要修改它,例如将 for next 循环替换为 for each r as mydataset.mytabledatarow in mydataset.mydatable 等

   For i = 1 To 10
        Dim l As New Label
        l.Location = New System.Drawing.Point With {.x = 10, .y = i * 30}
        Me.Controls.Add(l)
        l.Text = "test " & i.ToString
   Next