【问题标题】:How to check for multiple checked boxes in checkedlistbox如何检查checkedlistbox中的多个复选框
【发布时间】:2019-07-16 01:00:45
【问题描述】:

这是我想要实现的流程架构:

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

换句话说,我正在尝试查看是否检查了多个索引/项目。

我也想知道是否可以同时为多个选中列表框完成此任务。

【问题讨论】:

  • 我们需要更多信息,因为您说的是 Visual Studio,我假设是 .Net,但是 C# 或 Visual Basic 还是其他?您使用的是 WPF 还是 Windows 窗体?
  • Visual C#,使用 Windows 窗体
  • 使用CheckedItemsSelectedItems
  • Ken,我已经使用checkeditems 输出到列表框。我正在尝试查看是否有一种方法可以编写几行代码来确定在一组选择中是否存在索引或更好的多个索引。这就是我的想法:(x,y,z)是否存在于checkeditems或selected items中
  • @Sam,作为对@Ken 评论的补充,也许您需要改用CheckedListBox.CheckedIndices

标签: c# winforms checklistbox


【解决方案1】:

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

    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

【讨论】:

  • 谢谢巴尔塔萨克。我会试用它,并会在我想使用它的环境中回复您。
  • Baltasarq,我遇到了一点问题——你可能会说是自制的问题。我没有正确说明我使用的是什么语言。它实际上是 Visual Basic 而不是 C#。所以我尝试使用你的代码但无法成功执行。
  • Baltasarq,非常感谢,终于回到了我使用清单的编码问题。您的代码提供了我正在寻找的解决方案。但是我试图理解扩展方法——它是如何工作的,以便将其扩展到其他问题。谢谢。
  • 太棒了!您可以将答案标记为已接受吗?关于扩展方法,你可以在这里找到文档:docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多