共用方式為


HOW TO:判斷 Windows Form CheckedListBox 控制項中的已核取項目

更新:2007 年 11 月

當使用 Windows Form CheckedListBox 控制項顯示資料時,您可以逐一查看儲存在 CheckedItems 屬性中的集合,或使用 GetItemChecked 方法來逐步執行清單,以決定核取了哪些項目。GetItemChecked 方法會將項目索引編號當做它的引數,並傳回 true 或 false。SelectedItemsSelectedIndices 屬性不會決定要核取哪些項目 (這與您預期的可能相反),它們只會決定要反白顯示的項目。

若要在 CheckedListBox 控制項中決定核取的項目

  1. 因為集合是以零起始,所以從 0 開始逐一查看 CheckedItems 集合。請注意,這個方法會提供您核取項目清單 (而不是整個清單) 的項目編號。所以,如果清單中的第一個項目沒有被選取而第二個項目有被選取,則下列的程式碼會顯示類似 "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 - 1 ; 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.get_CheckedItems().get_Count() != 0  ) 
    {
       // If so, loop through all checked items and print results.
       System.String s = "";
       for(int x=0;x <= checkedListBox1.get_CheckedItems().get_Count() - 1;x++)
       {
          s = s + "Checked Item " + Convert.ToString(++x) + " = " +          checkedListBox1.get_CheckedItems().get_Item(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 - 1; 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;
    System.String s;
    s = "Checked items:\n";
    for(i = 0;i <= checkedListBox1.get_Items().get_Count() - 1;i ++)
    {
       if ( checkedListBox1.GetItemChecked(i)  ) 
       {
          s = s + "Item " + Convert.ToString(++i) + " = " + checkedListBox1.get_Item(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);
    

請參閱

其他資源

用來列出選項的 Windows Form 控制項