如何检查或取消选中VB.NET CheckedListBox控件中的所有项目

时间:2012-02-24 00:14:00

标签: vb.net

我需要选择和取消选择VB.NET CheckedListBox控件中的所有项目,最好的方法是什么?

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

        With clbCheckedListBox

        .Items.Add("Select/UnSelect All")
        .Items.Add("Enero")
        .Items.Add("Febrero")
        .Items.Add("Marzo")
        .Items.Add("Abril")
        .Items.Add("Mayo")
        .Items.Add("Junio")
        .Items.Add("Julio")
        .Items.Add("Agosto")
        .Items.Add("Septiembre")
        .Items.Add("Octubre")
        .Items.Add("Noviembre")
        .Items.Add("Diciembre")

        .SelectedIndex = 0

    End With


End Sub


    Private Sub clbCheckedListBox_ItemCheck(sender As Object, e As System.Windows.Forms.ItemCheckEventArgs) Handles clbCheckedListBox.ItemCheck

    If e.Index = 0 Then

        If e.NewValue = CheckState.Checked Then

            For idx As Integer = 1 To Me.clbCheckedListBox.Items.Count - 1
                Me.clbCheckedListBox.SetItemCheckState(idx, CheckState.Checked)
            Next

        ElseIf e.NewValue = CheckState.Unchecked Then

            For idx As Integer = 1 To Me.clbCheckedListBox.Items.Count - 1
                Me.clbCheckedListBox.SetItemCheckState(idx, CheckState.Unchecked)
            Next

        End If

    End If

End Sub

After Hours以上代码对我来说很好用!

8 个答案:

答案 0 :(得分:16)

你的意思是这样的:

Dim checked As Boolean = True   ' Set to True or False, as required.
For i As Integer = 0 To CheckedListBox1.Items.Count - 1
    CheckedListBox1.SetItemChecked(i, checked)
Next

这里我只是循环遍历所有CheckedListBox项并设置它们的检查状态。

答案 1 :(得分:5)

If button.Text = "Select All" Then
    For i As Integer = 0 To checklist.Items.Count - 1
        checklist.SetItemChecked(i, True)
    Next
    Button.Text = "Deselect All"
Else
    For i As Integer = 0 To checklist.Items.Count - 1
        checklist.SetItemChecked(i, False)
        Button.Text = "Select All"
    Next
End If

答案 2 :(得分:4)

里卡多,也许这可能就是你要找的东西:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim items$() = New String() {"Select/UnSelect All", "Enero",
     "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio",
     "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"}
    For Each Str As String In items : clbCheckedListBox.Items.Add(Str) : Next
End Sub ' Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs)

Private Sub clbCheckedListBox_ItemCheck(sender As System.Object, e As System.Windows.Forms.ItemCheckEventArgs) Handles clbCheckedListBox.ItemCheck
    If e.Index = 0 Then
        Dim newCheckedState As CheckState = e.NewValue
        For idx As Integer = 1 To clbCheckedListBox.Items.Count - 1
            Me.clbCheckedListBox.SetItemCheckState(idx, newCheckedState)
        Next
    End If
End Sub

答案 3 :(得分:2)

我发现clbCheckedListBox.clearSelection()适合取消所有选择。

答案 4 :(得分:1)

To check all CheckedListBox Item:

For i As Integer = 0 To CheckedListBox1.Items.Count - 1
     CheckedListBox1.SetItemChecked(i, True)
Next

To uncheck all CheckedListBox Item:

For i As Integer = 0 To CheckedListBox1.Items.Count - 1
     CheckedListBox1.SetItemChecked(i, false)
Next

答案 5 :(得分:1)

添加了一个名为" Select All"的单独checkbox。在选中或取消选中checkbox的{​​{1}}项时,可以选中或取消选中。因此,您可以在代码中的任何位置调用此checklistbox函数:

Kb()

答案 6 :(得分:1)

将此代码放入 SelectedValueChanged 事件中。

Private Sub clbCheckedListBox_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles ContrListCheckBox.SelectedValueChanged
    If clbCheckedListBox.SelectedIndex = 0 Then
        If clbCheckedListBox.GetItemChecked(0) = False Then
            For idx As Integer = 1 To clbCheckedListBox.Items.Count - 1

                Me.clbCheckedListBox.SetItemChecked(idx, False)
            Next
        Else
            For idx As Integer = 1 To ContrListCheckBox.Items.Count - 1

                Me.clbCheckedListBox.SetItemChecked(idx, True)
            Next
        End If
    ElseIf clbCheckedListBox.SelectedIndex > 0 Then
        If clbCheckedListBox.CheckedItems.Count = clbCheckedListBox.Items.Count - 1 And clbCheckedListBox.GetItemChecked(0) = False Then
            clbCheckedListBox.SetItemCheckState(0, CheckState.Checked)
        End If
        For idx As Integer = 1 To clbCheckedListBox.Items.Count - 1

            If clbCheckedListBox.GetItemChecked(idx) = False Then
                clbCheckedListBox.SetItemCheckState(0, CheckState.Unchecked)

            End If
        Next
    End If
End Sub

其他解决方案是正确的,但如果您想取消选中CheckBoxList中的另一个复选框或清空CheckBoxList而不使用Select All复选框 顶部的复选框将保持选中状态,因此上述代码应解决此问题。

答案 7 :(得分:0)

另一种选择是将检查/取消检查事件(例如两个按钮的检查/取消检查事件)捆绑到一个处理程序中,并使用 sender 的值设置检查状态:

Private Sub clbCheckedListBox(
    ByVal sender As Object,
    ByVal e As EventArgs) Handles btnAll.Click, btnNone.Click

    For i As Integer = 0 To clbApis.Items.Count - 1
        clbApis.SetItemChecked(i, sender Is btnAll)
    Next i
End Sub