检查在运行时创建的复选框的状态

时间:2014-09-27 05:40:36

标签: vb.net checkbox

我在Windows 7(64位)上使用VS2010

我在运行时创建了复选框,现在我陷入了无法检查状态的位置。单击按钮时,我想知道选中了哪些复选框..请指导我。

代码:

Public Class Form2

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim Data(10) As String
    Dim name(10) As String
    Dim i As Integer
    Dim offset = 10

    i = 0
    Data = Form1.cmail
    name = Form1.cname

    For Each cur In Data
        If cur Is vbNullString Then
            Exit For
        End If
        Dim checkBox = New CheckBox()
        Dim labelBox = New Label()
        Me.Controls.Add(checkBox)
        Me.Controls.Add(labelBox)
        checkBox.Location = New Point(10, offset)
        checkBox.Text = Data(i)
        checkBox.Checked = False
        checkBox.Size = New Size(150, 20)
        labelBox.Location = New Point(160, offset)
        labelBox.Text = name(i)
        labelBox.Size = New Size(1000, 20)
        offset = offset + 30
        i = i + 1
    Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub

结束班

1 个答案:

答案 0 :(得分:0)

您需要将这些文本框添加到列表中,这样您就可以在按钮单击中搜索选中的文本框。

Dim cheklist As New List(Of CheckBox)' creating list of checkboxes
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim Data(10) As String
    Dim name(10) As String
    Dim i As Integer
    Dim offset = 10
    i = 0
    Data = Form1.cmail
    name = Form1.cname
    For Each cur In Data
        If cur Is vbNullString Then
            Exit For
        End If
        Dim checkBox = New CheckBox()
        Dim labelBox = New Label()
        Me.Controls.Add(checkBox)
        Me.Controls.Add(labelBox)
        checkBox.Location = New Point(10, offset)
        checkBox.Text = Data(i)
        checkBox.Checked = False
        checkBox.Size = New Size(150, 20)
        cheklist.Add(checkBox) ' add created check box to the ckeckboxList
        labelBox.Location = New Point(160, offset)
        labelBox.Text = name(i)
        labelBox.Size = New Size(1000, 20)
        offset = offset + 30
        i = i + 1
    Next
End Sub
'*****Display the textboxes which are checked****
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each chk As CheckBox In cheklist
            If chk.Checked = True Then
                MsgBox(chk.Text & "is checked")
            End If
        Next
    End Sub