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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9423510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:57:34  来源:igfitidea点击:

How to Check or Uncheck all Items in VB.NET CheckedListBox Control

vb.net

提问by RicardoBalda

I need to select and unselect all items in a VB.NET CheckedListBoxcontrol, what is the best way to do this?

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

    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 the above code work fine for me !

下班后,上面的代码对我来说很好用!

采纳答案by Edward

Ricardo, perhaps this might be what you are looking for:

里卡多,也许这可能是您正在寻找的:

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

回答by Jay Riggs

Do you mean something like this:

你的意思是这样的:

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

Here I'm just looping through all the CheckedListBox Items and setting their checked state.

在这里,我只是遍历所有 CheckedListBox 项并设置它们的选中状态。

回答by Avinash

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

回答by Trey

I found that clbCheckedListBox.clearSelection()works well for unselecting all.

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

回答by Rashedul.Rubel

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

回答by Rashedul.Rubel

Added a separate checkboxcalled "Select All". On checking and unchecking of this checkboxitems of a checklistboxcan be selected or unselected. So you can call this Kb()function anywhere in your code:

添加了一个checkbox名为“全选”的单独内容。在勾选和取消勾选此项时checkboxchecklistbox可以选择或取消选择。所以你可以Kb()在代码的任何地方调用这个函数:

Private Sub ChkSelectAll_Click(sender As Object, e As EventArgs) Handles ChkSelectAll.Click
    Kb(ChkSelectAll.CheckState)
End Sub

Private Sub Kb(ByVal Key As Boolean)
    For i As Integer = 0 To ChkLstServices.Items.Count - 1
        ChkLstServices.SetItemChecked(i, Key)
    Next
End Sub

回答by Ahmed Ramdan

Put this code in the SelectedValueChangedevent.

将此代码放在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

Other solutions are correct but if you want to deselect another checkbox inside the CheckBoxList or empty the CheckBoxList without the Select Allcheckbox the top checkbox will remain checked and it is not logical so the above code should solve this problem.

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