VB.net中的CheckBox数组

时间:2010-03-05 05:43:17

标签: .net vb.net

我需要有足够的checkBoxes&我的vb.net表单中的textBoxes。

所以我真的需要CheckBox数组来减少事件的编码。

但是我无法像在VB-6中那样创建它们的数组

另外,我只需要在设计时制作它们。 所以在编写类似

的编写时
dim chkBox as new CheckBox()

然后设置位置&他们每个人的文字对我来说都不可行bcoz我需要在我的表格中接近约100个。

那么请帮助我创建他们的阵列?

3 个答案:

答案 0 :(得分:4)

也许您应该使用CheckedListBox代替

答案 1 :(得分:1)

先生。特加斯...

希望这段代码对你有用......

假设已在Designer

中创建“OneTextObject”(文本框)
Dim aTextObject() As TextBox
Dim theObjectCount% = 0
表单加载中的

ReDim aTextObject(0)
aTextObject(0) = Me.OneTextObject

然后当你想要添加到这个“数组”....

theObjectCount += 1
    ReDim Preserve aTextObject(theObjectCount)
    aTextObject(theObjectCount) = New TextBox
    Me.Controls.Add(aTextObject(theObjectCount))
    AddHandler aTextObject(theObjectCount).DoubleClick, AddressOf aTextObject_Click
    AddHandler aTextObject(theObjectCount).MouseMove, AddressOf aTextObject_MouseMove
    AddHandler aTextObject(theObjectCount).MouseDown, AddressOf aTextObject_MouseDown
    aTextObject(theObjectCount).ContextMenu = New ContextMenu
aTextObject(theObjectCount).Location = New System.Drawing.Point(some_x, some_y)
    aTextObject(theObjectCount).Tag = "You can use this TAG to identify this TextBox vs all the others...  |  Item#1"  ' note the PIPE "|" symbol ... it can be utilized later.

    aTextObject(theObjectCount).Text = "Whatever"
    aTextObject(theObjectCount).Visible = True
    aTextObject(theObjectCount).BringToFront()
    aTextObject(theObjectCount).TextAlign =  HorizontalAlignment.Left

aTextObject(theObjectCount).Width = some_width
    aTextObject(theObjectCount).Height = some_height
    aTextObject(theObjectCount).Refresh()

然后是一些创建textBox的回调示例....请注意,没有HANDLES短语!!!!!

 Public Sub aTextObject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim theParse() As String

    theParse = sender.tag.Split("|") 

Select Case theParse(1) 'which ITEM#

    Case "Item#1"

    Case Else

End Select

End Sub

'此示例使用CType(发件人)机制......

Public Sub aTextObject_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    Dim ltextbox As TextBox

    ltextbox = CType(sender, TextBox)

'do something with ltextbox...

Dim theParse() As String

    theParse = ltextbox.tag.Split("|") 

Select Case theParse(1) 'which ITEM#

    Case "Item#1"

    Case Else

End Select

End Sub



Public Sub aTextObject_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    Dim ltextbox As TextBox

    ltextbox = CType(sender, TextBox)

'do something with ltextbox...

Dim theParse() As String

    theParse = ltextbox.tag.Split("|") 

Select Case theParse(1) 'which ITEM#

    Case "Item#1"

    Case Else

End Select



End Sub

答案 2 :(得分:0)

先生。特加斯...

这在这里基本上回答了你的问题:

About using arguments passed to the CheckedChanged event of Checkbox