Visual Basic 2010 Creating a Control Array

后端 未结 5 1253
你的背包
你的背包 2020-12-21 09:03

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 a

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-21 09:24

    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.

    The TAG property is used to store and retrieve the idex of the text box.

提交回复
热议问题