VB.NET中的复选框数组

VB.NET中的复选框数组,vb.net,for-loop,checkbox,controls,Vb.net,For Loop,Checkbox,Controls,我现在可以在表单上画多个复选框,但是我不确定如何单独选中每个复选框以查看是否已选中。 这是我用来在屏幕上画复选框的代码 Dim data as String() = New String() { "testing", "testing2" } Dim offset = 10 For Each cur In data Dim checkBox = New CheckBox() Me.Controls.Add(checkBox)

我现在可以在表单上画多个复选框,但是我不确定如何单独选中每个复选框以查看是否已选中。 这是我用来在屏幕上画复选框的代码

    Dim data as String() = New String() { "testing", "testing2" }
    Dim offset = 10
    For Each cur In data
        Dim checkBox = New CheckBox()
        Me.Controls.Add(checkBox)
        checkBox.Location = New Point(10, offset)
        checkBox.Text = cur
        checkBox.Checked = True
        checkBox.Size = New Size(100, 20)
        offset = offset + 20
    Next

要检索动态添加的复选框,可以在表单控件集合上循环

For Each chk In Me.Controls.OfType(Of CheckBox)()
   if chk.Checked Then
      if chk.Name = "testing" Then
          ' code for testing.checked = true
      Else if chk.Name = "testing2" then
          ' code for testing2.checked = true
      End If
   End If
Next

您可以将对复选框的引用存储在(复选框的)列表中:

但是,如果希望使其更通用,以便可以有多个复选框集,并可以将它们添加到所选的容器控件中,则可以使其更复杂,并有一个类来管理一组复选框。例如:

Option Infer On

Public Class Form1

    Dim myCheckboxes As Dictionary(Of String, CheckboxOrganiser)

    'TODO: Consider putting this CheckboxOrganiser class in its own file.
    Public Class CheckboxOrganiser
        Implements IDisposable

        Property Checkboxes As List(Of CheckBox)

        ' ########### some examples of what could be in this Class
        Sub ShowCheckedCount()
            Dim count = 0
            For Each cb In Checkboxes
                ' do something with cb.Checked
                If cb.Checked Then
                    count += 1
                End If
            Next

            If count = 1 Then
                MsgBox("There is 1 checked.")
            Else
                MsgBox("There are " & count.ToString() & " checked.")
            End If

        End Sub

        Sub ShowIfChecked(tagText As String)
            For Each cb In Checkboxes
                If cb.Tag.ToString = tagText Then
                    MsgBox(String.Format("{0} is {1}checked.", tagText, If(cb.Checked, "", "not ")))
                    Exit For
                End If
            Next

        End Sub
        ' ########### end examples

        ' dispose of the CheckBoxes and the references to them
        Sub Clear()
            If Me.Checkboxes IsNot Nothing Then
                For Each cb In Me.Checkboxes
                    cb.Parent.Controls.Remove(cb)
                    cb.Dispose()
                Next
                Me.Checkboxes.Clear()
            End If

        End Sub

        ' add the CheckBoxes to a container control
        Sub Display(target As Control)
            'TODO: check that target is a container control
            target.Controls.AddRange(Me.Checkboxes.ToArray())

        End Sub

        ' make a new list of CheckBoxes
        Sub New(left As Integer, top As Integer, data As String())
            Checkboxes = New List(Of CheckBox)
            Dim cbSize As New Size(100, 20)

            Dim offset = top
            For Each cur In data
                Dim cb = New CheckBox()
                cb.Location = New Point(left, offset)
                cb.Text = cur
                cb.Tag = cur
                cb.Checked = True
                cb.Size = cbSize
                Checkboxes.Add(cb)
                offset = offset + 20
            Next

        End Sub

        Sub New()
            Checkboxes = New List(Of CheckBox)

        End Sub

        ' We're trying to do this properly, so it is best to implement the code for .Dispose()...
#Region "IDisposable Support"
        Private disposedValue As Boolean ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    Me.Clear()
                End If
            End If
            Me.disposedValue = True
        End Sub

        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class

    ' an example of getting rid of a set of CheckBoxes
    Private Sub bnRemoveTestCbs_Click(sender As Object, e As EventArgs) Handles bnRemoveTestCbs.Click
        myCheckboxes("tests").Dispose()

    End Sub

    ' an example of doing something with the checkboxes
    Private Sub bnCountThem_Click(sender As Object, e As EventArgs) Handles bnCountThem.Click
        myCheckboxes("shapes").ShowCheckedCount()

    End Sub

    ' inspect one checkbox in a collection
    Private Sub bnIsTriangleChecked_Click(sender As Object, e As EventArgs) Handles bnIsTriangleChecked.Click
        myCheckboxes("shapes").ShowIfChecked("Triangles")

    End Sub

    Sub Demo()
        ' use a Dictionary so that each CheckboxOrganiser can be referred to by a name
        myCheckboxes = New Dictionary(Of String, CheckboxOrganiser)

        ' create a set of checkboxes and name it "testing"...
        Dim data = New String() {"testing", "testing2"}
        myCheckboxes.Add("tests", New CheckboxOrganiser(10, 10, data))

        ' show them on the main Form
        myCheckboxes("tests").Display(Me)

        ' and another set of checkboxes...
        data = {"Triangles", "Squares"}
        myCheckboxes.Add("shapes", New CheckboxOrganiser(10, 20, data))

        ' we're going to add them to a GroupBox instead:
        Dim gb As New GroupBox With {.Left = 120, .Top = 20, .Width = 120, .Height = 80, .Text = "Shapes"}
        Me.Controls.Add(gb)
        myCheckboxes("shapes").Display(gb)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Demo()

    End Sub


End Class

我开始有点忘乎所以了,但我认为值得向您展示的是,通过更多的代码开始,您可以使其他东西更易于使用。

如果您想动态创建多个复选框,则需要逐个创建它们(或使用循环)@Steve我已经更新了用于添加复选框的代码,现在唯一的问题是如何检查复选框是否已被选中。您可以将复选框添加到控件集合中。要检索它们,只需检查该集合。然而,真正的问题是,如果复选框被选中或未选中,您想做什么。如果每个复选框都是相同的,那么您就不会有问题。否则,您需要在复选框中添加一些ifname@Steve复选框不会全部被选中或全部被取消选中,我如何才能检查收藏?请勾选(双关语)下面的答案。
Option Infer On

Public Class Form1

    Dim theCheckBoxes As List(Of CheckBox)

    Sub SetUpCheckBoxes()
        theCheckBoxes = New List(Of CheckBox)

        Dim data As String() = New String() {"testing", "testing2"}
        Dim offset = 10
        For Each cur In data
            Dim cb = New CheckBox()
            cb.Location = New Point(10, offset)
            cb.Text = cur
            cb.Checked = True
            cb.Size = New Size(100, 20)
            Me.Controls.Add(cb)
            theCheckBoxes.Add(cb)
            offset = offset + 20
        Next

    End Sub

    ' an example of doing something with the list of checkboxes
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim count = 0
        For Each cb In theCheckBoxes
            ' do something with cb.Checked
            If cb.Checked Then
                count += 1
            End If
        Next

        If count = 1 Then
            MsgBox("There is 1 checked.")
        Else
            MsgBox("There are " & count.ToString() & " checked.")
        End If

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SetUpCheckBoxes()

    End Sub

End Class
Option Infer On

Public Class Form1

    Dim myCheckboxes As Dictionary(Of String, CheckboxOrganiser)

    'TODO: Consider putting this CheckboxOrganiser class in its own file.
    Public Class CheckboxOrganiser
        Implements IDisposable

        Property Checkboxes As List(Of CheckBox)

        ' ########### some examples of what could be in this Class
        Sub ShowCheckedCount()
            Dim count = 0
            For Each cb In Checkboxes
                ' do something with cb.Checked
                If cb.Checked Then
                    count += 1
                End If
            Next

            If count = 1 Then
                MsgBox("There is 1 checked.")
            Else
                MsgBox("There are " & count.ToString() & " checked.")
            End If

        End Sub

        Sub ShowIfChecked(tagText As String)
            For Each cb In Checkboxes
                If cb.Tag.ToString = tagText Then
                    MsgBox(String.Format("{0} is {1}checked.", tagText, If(cb.Checked, "", "not ")))
                    Exit For
                End If
            Next

        End Sub
        ' ########### end examples

        ' dispose of the CheckBoxes and the references to them
        Sub Clear()
            If Me.Checkboxes IsNot Nothing Then
                For Each cb In Me.Checkboxes
                    cb.Parent.Controls.Remove(cb)
                    cb.Dispose()
                Next
                Me.Checkboxes.Clear()
            End If

        End Sub

        ' add the CheckBoxes to a container control
        Sub Display(target As Control)
            'TODO: check that target is a container control
            target.Controls.AddRange(Me.Checkboxes.ToArray())

        End Sub

        ' make a new list of CheckBoxes
        Sub New(left As Integer, top As Integer, data As String())
            Checkboxes = New List(Of CheckBox)
            Dim cbSize As New Size(100, 20)

            Dim offset = top
            For Each cur In data
                Dim cb = New CheckBox()
                cb.Location = New Point(left, offset)
                cb.Text = cur
                cb.Tag = cur
                cb.Checked = True
                cb.Size = cbSize
                Checkboxes.Add(cb)
                offset = offset + 20
            Next

        End Sub

        Sub New()
            Checkboxes = New List(Of CheckBox)

        End Sub

        ' We're trying to do this properly, so it is best to implement the code for .Dispose()...
#Region "IDisposable Support"
        Private disposedValue As Boolean ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    Me.Clear()
                End If
            End If
            Me.disposedValue = True
        End Sub

        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class

    ' an example of getting rid of a set of CheckBoxes
    Private Sub bnRemoveTestCbs_Click(sender As Object, e As EventArgs) Handles bnRemoveTestCbs.Click
        myCheckboxes("tests").Dispose()

    End Sub

    ' an example of doing something with the checkboxes
    Private Sub bnCountThem_Click(sender As Object, e As EventArgs) Handles bnCountThem.Click
        myCheckboxes("shapes").ShowCheckedCount()

    End Sub

    ' inspect one checkbox in a collection
    Private Sub bnIsTriangleChecked_Click(sender As Object, e As EventArgs) Handles bnIsTriangleChecked.Click
        myCheckboxes("shapes").ShowIfChecked("Triangles")

    End Sub

    Sub Demo()
        ' use a Dictionary so that each CheckboxOrganiser can be referred to by a name
        myCheckboxes = New Dictionary(Of String, CheckboxOrganiser)

        ' create a set of checkboxes and name it "testing"...
        Dim data = New String() {"testing", "testing2"}
        myCheckboxes.Add("tests", New CheckboxOrganiser(10, 10, data))

        ' show them on the main Form
        myCheckboxes("tests").Display(Me)

        ' and another set of checkboxes...
        data = {"Triangles", "Squares"}
        myCheckboxes.Add("shapes", New CheckboxOrganiser(10, 20, data))

        ' we're going to add them to a GroupBox instead:
        Dim gb As New GroupBox With {.Left = 120, .Top = 20, .Width = 120, .Height = 80, .Text = "Shapes"}
        Me.Controls.Add(gb)
        myCheckboxes("shapes").Display(gb)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Demo()

    End Sub


End Class