vb.net Visual Basic 2010 创建控件数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3795253/
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
Visual Basic 2010 Creating a Control Array
提问by jSherz
How can I make a control array? Or the equivalent.
如何制作控件数组?或等价物。
I am used to Visual Basic 6 which presents the option of whether to create a control array when I copy and paste and control.
我习惯了 Visual Basic 6,它提供了在我复制粘贴和控制时是否创建控件数组的选项。
回答by Brad
回答by mark
Another implicit feature of the control arrays that is easily overlooked is the association of numeric indexes with each element of the array. The indexes can be any positive int as long as they areunique in the collection; they do not have to be sequential. Consequently the array is more like a dictionary indexed by integers with each item value being an instance of a specific type of control.
控件数组的另一个隐含特性很容易被忽视,即数字索引与数组的每个元素的关联。索引可以是任何正整数,只要它们在集合中是唯一的;它们不必是连续的。因此,数组更像是一个由整数索引的字典,每个项目值都是特定类型控件的实例。
Logic in the VB6 event handlers for the control array get the value of the index along with the attributes of the event to be handled. Logic in the handler typically uses the index to determine which instance of the control was raising the event.
控件数组的 VB6 事件处理程序中的逻辑获取索引值以及要处理的事件的属性。处理程序中的逻辑通常使用索引来确定引发事件的控件实例。
.NET event handlers are quite different. You will typically get a reference to a specific control instance and an instance of a specific event object with the attributes of the event to be handled. You will NOT get the index.
.NET 事件处理程序完全不同。您通常会获得对特定控件实例的引用,以及具有要处理的事件属性的特定事件对象的实例。你不会得到索引。
Also, VB6 apps sometimes have logic that iterates/manipulates the control array as an array.
此外,VB6 应用程序有时具有将控件数组作为数组迭代/操作的逻辑。
In our default translations, we try to support legacy VB6 logic that depends explicitly on the control array and its indexes. We rewrite control arrays as a group of individual control instances and then we add them to a generic OrderDictionary<int, controlType> during form initialization. The individual controls subscribe to the events and we can use the collection to lookup the index given a control instance or to iterate/manipulate the items in the "array". If you do not explicitly need the index or the array, you can get rid of the collection.
在我们的默认翻译中,我们尝试支持显式依赖于控件数组及其索引的传统 VB6 逻辑。我们将控件数组重写为一组单独的控件实例,然后在表单初始化期间将它们添加到通用 OrderDictionary<int, controlType> 中。各个控件订阅事件,我们可以使用集合来查找给定控件实例的索引或迭代/操作“数组”中的项目。如果您不明确需要索引或数组,则可以去掉集合。
Dynamically adding controls is more work now -- it was conceptually like adding an item to the control array and supported with a single statement in VB6 (Load control). As far as I know, in .NET you need to clone a control, deep copy the properties explicitly, and hook up the event handlers explicitly. It can be generalized using reflection and other moderately advanced techniques -- but it sure is not simply calling "load control". If there is an easier way to do this in .NET I would love to hear about it. FWIW: we translate to a helper function.
现在动态添加控件的工作量更大——它在概念上就像向控件数组添加一个项目,并且在 VB6(加载控件)中使用单个语句支持。据我所知,在 .NET 中,您需要克隆一个控件,显式地深度复制属性,并显式地连接事件处理程序。它可以使用反射和其他中等高级技术进行推广——但它肯定不是简单地称为“负载控制”。如果在 .NET 中有更简单的方法可以做到这一点,我很想听听。FWIW:我们转换为辅助函数。
回答by sandeep sharma
there are three ways to create control array.
有三种方法可以创建控件数组。
- assign the same name to more then one control.
- copy an evangelizing control and past it into the form.
- set the index properly to a value that is not null.
- 将相同的名称分配给多个控件。
- 复制一个传播控制并将其传递到表单中。
- 将索引正确设置为非空值。
回答by cesare
You can act only through code. For example:
您只能通过代码采取行动。例如:
Dim c() As TextBox
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim j As Integer
For j = 0 To 10
ReDim Preserve c(j)
c(j) = New TextBox
c(j).Name = "txt" & j
c(j).Parent = Me
c(j).Top = j * c(j).PreferredHeight + 2
c(j).Tag = j
c(j).Visible = True
AddHandler c(j).KeyPress, AddressOf TestKeyPress
Next
End Sub
Public Sub TestKeyPress(source As Object, e As KeyPressEventArgs)
Dim index As Integer
index = CInt(source.tag)
If index >= 5 Then
If e.KeyChar > "9" Or e.KeyChar < "0" Then
e.Handled = True
End If
Else
If e.KeyChar <= "9" And e.KeyChar >= "0" Then
e.Handled = True
End If
End If
End Sub
This will create eleven text boxes assigning to all the same event handler.
这将创建 11 个分配给所有相同事件处理程序的文本框。
The TAG property is used to store and retrieve the idex of the text box.
TAG 属性用于存储和检索文本框的 idex。
回答by GSerg
A control array in VB6 solely existed to allow a single event handler for multiple controls of same type.
VB6 中的控件数组仅存在于允许同一类型的多个控件使用单个事件处理程序。
You do this in VB.NET by putting the controls into the Handles
clause:
您可以在 VB.NET 中通过将控件放入Handles
子句中来执行此操作:
private sub Button_Click(...) Handles Command1.Click, Command2.Click, Command3.Click
end sub