如何检查复选框列表中的多个复选框

时间:2019-02-22 00:58:24

标签: c# winforms checklistbox

这是我要实现的过程的模式:

从复选框列表中选择选项---->将选中项目的索引与特定索引集进行比较。

换句话说,我试图查看是否已检查多个索引/项目。

我还想知道是否可以同时针对多个清单框完成此任务。

1 个答案:

答案 0 :(得分:0)

没有一种自动方法可以为您提供所需的信息。但是,您当然可以自己滚动。例如,可以创建一个扩展方法来检查您提供给它的索引是否存在。

    static class CheckedListBoxExtension {
        public static bool ContainsSet(this WinForms.CheckedListBox cbl, ICollection<int> values)
        {
            var valueSet = new HashSet<int>( values );

            foreach(int index in cbl.CheckedIndices) {
                if ( valueSet.Contains( index ) ) {
                    valueSet.Remove( index );

                }
            }

            return valueSet.Count == 0;
        }
    }

然后您可以通过以下方式使用它:

if ( this.checkBoxes.ContainsSet( new int[]{ 2, 4 } ) ) {
            // Your code here...
}

一个完整的示例如下:

class Window: WinForms.Form {
    public Window()
    {
        this.Build();
        this.MinimumSize = new Drawing.Size( 320, 200 );
    }

    void Build()
    {
        this.status = new WinForms.TextBox { Dock = WinForms.DockStyle.Bottom };
        this.checkBoxes = new WinForms.CheckedListBox {
            Dock = WinForms.DockStyle.Fill 
        };

        for(int i = 1; i <= 10; ++i) {
            this.checkBoxes.Items.Add(
                char.ToString( (char) ( 'a' + i ) ),
                false
            );
        }

        this.checkBoxes.SelectedIndexChanged += (sender, e) => this.UpdateCheckedList();
        this.Controls.Add( this.checkBoxes );
        this.Controls.Add( this.status );
    }

    void UpdateCheckedList()
    {
        string strStatus = "";

        if ( this.checkBoxes.ContainsSet( new int[]{ 2, 4 } ) ) {
            strStatus += "YES";
        }

        this.status.Text = strStatus;
    }

    WinForms.CheckedListBox checkBoxes;
    WinForms.TextBox status;
}

static class CheckedListBoxExtension {
    public static bool ContainsSet(this WinForms.CheckedListBox cbl, ICollection<int> values)
    {
        var valueSet = new HashSet<int>( values );

        foreach(int index in cbl.CheckedIndices) {
            if ( valueSet.Contains( index ) ) {
                valueSet.Remove( index );

            }
        }

        return valueSet.Count == 0;
     }
}

希望这会有所帮助。

(PS。随后是Visual Basic.NET)

Imports System.Runtime.CompilerServices

Class Window
    Inherits WinForms.Form

    Public Sub New()
        Me.Build()
        Me.MinimumSize = New Drawing.Size(320, 200)
    End Sub

    Private Sub Build()
        Me.status = New WinForms.TextBox With {
            .Dock = WinForms.DockStyle.Bottom
        }
        Me.checkBoxes = New WinForms.CheckedListBox With {
            .Dock = WinForms.DockStyle.Fill
        }

        For i As Integer = 1 To 10
            Me.checkBoxes.Items.Add(Char.ToString(ChrW(("a"c + i))), False)
        Next

        Me.checkBoxes.SelectedIndexChanged += Function(sender, e) Me.UpdateCheckedList()
        Me.Controls.Add(Me.checkBoxes)
        Me.Controls.Add(Me.status)
    End Sub

    Private Sub UpdateCheckedList()
        Dim strStatus As String = ""

        If Me.checkBoxes.ContainsSet(New Integer() {2, 4}) Then
            strStatus += "YES"
        End If

        Me.status.Text = strStatus
    End Sub

    Private checkBoxes As WinForms.CheckedListBox
    Private status As WinForms.TextBox
End Class

Module CheckedListBoxExtension
    <Extension()>
    Function ContainsSet(ByVal cbl As WinForms.CheckedListBox, ByVal values As ICollection(Of Integer)) As Boolean
        Dim valueSet = New HashSet(Of Integer)(values)

        For Each index As Integer In cbl.CheckedIndices

            If valueSet.Contains(index) Then
                valueSet.Remove(index)
            End If
        Next

        Return valueSet.Count = 0
    End Function
End Module