Share via


Procedura: rilevare gli elementi selezionati nel controllo CheckedListBox di Windows Form

Aggiornamento: novembre 2007

Per la presentazione di dati in un controllo CheckedListBox di Windows Form è possibile scorrere l'insieme memorizzato nella proprietà CheckedItems o esaminare l'elenco utilizzando il metodo GetItemChecked per rilevare gli elementi selezionati. Il metodo GetItemChecked accetta il numero di indice di un elemento come argomento e restituisce true o false. Le proprietà SelectedItems e SelectedIndices, contrariamente a quanto si potrebbe pensare, non rilevano gli elementi selezionati, ma quelli evidenziati.

Per rilevare gli elementi selezionati in un controllo CheckedListBox

  1. Scorrere l'insieme CheckedItems partendo da 0, trattandosi di un insieme a base zero. Si noti che il metodo fornirà il numero dell'elemento nell'elenco degli elementi selezionati, non nell'intero elenco. Di conseguenza, se il primo elemento dell'elenco non è selezionato e il secondo sì, nel codice che segue verrà visualizzato un testo analogo a "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);
    }
    

    - oppure -

  2. Scorrere l'insieme Items partendo da 0, trattandosi di un insieme a base zero, e chiamare il metodo GetItemChecked per ogni elemento. Tenere presente che questo metodo fornirà il numero dell'elemento nell'intero elenco. Di conseguenza, se il primo elemento dell'elenco non è selezionato e il secondo sì, verrà visualizzato un testo analogo a "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);
    

Vedere anche

Altre risorse

Controlli Windows Form usati per elencare opzioni