共用方式為


如何:判斷 Windows Form CheckedListBox 控制項中的已核取項目

在 Windows Forms CheckedListBox 控制項中呈現資料時,您可以逐一查看儲存在 CheckedItems 屬性中的集合,或使用 GetItemChecked 方法來逐步執行清單,以判斷要核取哪些項目。 GetItemChecked 方法會採用項目索引編號作為其引數,並傳回 truefalse。 與您預期的可能相反,SelectedItemsSelectedIndices 屬性不會判斷要核取哪些項目;而是判斷要醒目提示哪些項目。

判斷 CheckedListBox 控制項中的已核取項目

  1. 逐一查看 CheckedItems 集合,從 0 開始,因為集合是以零為起始。 請注意,此方法會提供已核取項目清單中的項目編號,而不是整體清單。 因此,如果未核取清單中的第一個項目並核取第二個項目,下列程式碼會顯示如 "Checked Item 1 = MyListItem2" 之類的文字。

    ' Determine if there are any items checked.  
    If CheckedListBox1.CheckedItems.Count <> 0 Then  
       ' If so, loop through all checked items and print results.  
       Dim x As Integer  
       Dim s As String = ""  
       For x = 0 To CheckedListBox1.CheckedItems.Count - 1  
          s = s & "Checked Item " & (x + 1).ToString & " = " & CheckedListBox1.CheckedItems(x).ToString & ControlChars.CrLf  
       Next x  
       MessageBox.Show(s)  
    End If  
    
    // Determine if there are any items checked.  
    if(checkedListBox1.CheckedItems.Count != 0)  
    {  
       // If so, loop through all checked items and print results.  
       string s = "";  
       for(int x = 0; x < checkedListBox1.CheckedItems.Count ; x++)  
       {  
          s = s + "Checked Item " + (x+1).ToString() + " = " + checkedListBox1.CheckedItems[x].ToString() + "\n";  
       }  
       MessageBox.Show(s);  
    }  
    
    // Determine if there are any items checked.  
    if(checkedListBox1->CheckedItems->Count != 0)  
    {  
       // If so, loop through all checked items and print results.  
       String ^ s = "";  
       for(int x = 0; x < checkedListBox1->CheckedItems->Count; x++)  
       {  
          s = String::Concat(s, "Checked Item ", (x+1).ToString(),  
             " = ", checkedListBox1->CheckedItems[x]->ToString(),  
             "\n");  
       }  
       MessageBox::Show(s);  
    }  
    
    • 或-
  2. 逐步執行 Items 集合,從 0 開始,因為集合是以零為起始,並針對每個項目呼叫 GetItemChecked 方法。 請注意,這個方法會提供您整體清單中的項目編號,因此,如果未核取清單中的第一個項目並核取第二個項目,則會顯示類似 "Item 2 = MyListItem2" 的內容。

    Dim i As Integer  
    Dim s As String  
    s = "Checked Items:" & ControlChars.CrLf  
    For i = 0 To (CheckedListBox1.Items.Count - 1)  
       If CheckedListBox1.GetItemChecked(i) = True Then  
          s = s & "Item " & (i + 1).ToString & " = " & CheckedListBox1.Items(i).ToString & ControlChars.CrLf  
       End If  
    Next  
    MessageBox.Show(s)  
    
    int i;  
    string s;
    s = "Checked items:\n" ;  
    for (i = 0; i <= (checkedListBox1.Items.Count-1); i++)  
    {  
       if (checkedListBox1.GetItemChecked(i))  
       {  
          s = s + "Item " + (i+1).ToString() + " = " + checkedListBox1.Items[i].ToString() + "\n";  
       }  
    }  
    MessageBox.Show (s);  
    
    int i;  
    String ^ s;
    s = "Checked items:\n" ;  
    for (i = 0; i <= (checkedListBox1->Items->Count-1); i++)  
    {  
       if (checkedListBox1->GetItemChecked(i))  
       {  
          s = String::Concat(s, "Item ", (i+1).ToString(), " = ",  
             checkedListBox1->Item[i]->ToString(), "\n");  
       }  
    }  
    MessageBox::Show(s);  
    

另請參閱