如何制作控件数组?或者它的等效方式。
我习惯于使用Visual Basic 6,当我复制和粘贴控件时,它会提供创建控件数组的选项。
创建控件数组有三种方法:
private sub Button_Click(...) Handles Command1.Click, Command2.Click, Command3.Click
end sub
你只能通过代码来实现行动。例如:
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
这将创建十一个文本框,并分配给它们相同的事件处理程序。
TAG 属性用于存储和检索文本框的索引。