[英]How to check if a particular entry in a CheckedListBox is checked
[英]How to check for multiple checked boxes in checkedlistbox
這是我要實現的過程的架構:
從復選框列表中選擇選項---->將選中項目的索引與特定索引集進行比較。
換句話說,我試圖查看是否已檢查多個索引/項目。
我也想知道是否可以同時為多個清單框實現此任務。
沒有自動的方法可以為您提供所需的信息。 但是,您當然可以自己滾動。 例如,可以創建一個擴展方法,以檢查您提供給它的索引是否存在。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.