Metodo ListBox.SetSelected
Aggiornamento: novembre 2007
Seleziona o deseleziona l'elemento specificato nel controllo ListBox.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
public void SetSelected( int index, boolean value )
public function SetSelected( index : int, value : boolean )
Parametri
- index
- Tipo: System.Int32
Indice in base zero dell'elemento nel controllo ListBox da selezionare o deselezionare.
- value
- Tipo: System.Boolean
true per selezionare l'elemento specificato; in caso contrario, false.
| Eccezione | Condizione |
|---|---|
| ArgumentOutOfRangeException | L'indice specificato non era compreso nell'intervallo di valori validi. |
| InvalidOperationException | La proprietà SelectionMode è stata impostata su None. |
È possibile utilizzare questa proprietà per impostare la selezione di elementi in un controllo ListBox a selezione multipla. Per selezionare un elemento in un controllo ListBox a selezione singola, utilizzare la proprietà SelectedIndex.
Nell'esempio di codice riportato di seguito viene illustrato come creare un controllo ListBox che visualizza più elementi nelle colonne e consente di selezionare più elementi nel relativo elenco. Nel codice di esempio vengono aggiunti 50 elementi al controllo ListBox utilizzando il metodo Add della classe ListBox.ObjectCollection, quindi vengono selezionati tre elementi dall'elenco utilizzando il metodo SetSelected. Quindi, vengono visualizzati i valori dell'insieme ListBox.SelectedObjectCollection (attraverso la proprietà SelectedItems) e ListBox.SelectedIndexCollection (attraverso la proprietà SelectedIndices). Per eseguire questo esempio è necessario che il codice si trovi internamente e venga chiamato da un oggetto Form.
private void button1_Click(object sender, System.EventArgs e) { // Create an instance of the ListBox. ListBox listBox1 = new ListBox(); // Set the size and location of the ListBox. listBox1.Size = new System.Drawing.Size(200, 100); listBox1.Location = new System.Drawing.Point(10,10); // Add the ListBox to the form. this.Controls.Add(listBox1); // Set the ListBox to display items in multiple columns. listBox1.MultiColumn = true; // Set the selection mode to multiple and extended. listBox1.SelectionMode = SelectionMode.MultiExtended; // Shutdown the painting of the ListBox as items are added. listBox1.BeginUpdate(); // Loop through and add 50 items to the ListBox. for (int x = 1; x <= 50; x++) { listBox1.Items.Add("Item " + x.ToString()); } // Allow the ListBox to repaint and display the new items. listBox1.EndUpdate(); // Select three items from the ListBox. listBox1.SetSelected(1, true); listBox1.SetSelected(3, true); listBox1.SetSelected(5, true); // Display the second selected item in the ListBox to the console. System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems[1].ToString()); // Display the index of the first selected item in the ListBox. System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices[0].ToString()); }
private void button1_Click(Object sender, System.EventArgs e)
{
// Create an instance of the ListBox.
ListBox listBox1 = new ListBox();
// Set the size and location of the ListBox.
listBox1.set_Size(new System.Drawing.Size(200,100));
listBox1.set_Location(new System.Drawing.Point(10,10));
// Add the ListBox to the form.
this.get_Controls().Add(listBox1);
// Set the ListBox to display items in multiple columns.
listBox1.set_MultiColumn(true);
// Set the selection mode to multiple and extended.
listBox1.set_SelectionMode(SelectionMode.MultiExtended);
// Shutdown the painting of the ListBox as items are added.
listBox1.BeginUpdate();
// Loop through and add 50 items to the ListBox.
for (int x = 1; x <= 50; x++) {
listBox1.get_Items().Add(("Item" + (new Integer(x)).ToString()));
}
// Allow the ListBox to repaint and display the new items.
listBox1.EndUpdate();
// Select three items from the ListBox.
listBox1.SetSelected(1,true);
listBox1.SetSelected(3,true);
listBox1.SetSelected(5,true);
// Display the second selected item in the ListBox to the console.
System.Diagnostics.Debug.WriteLine
(listBox1.get_SelectedItems().get_Item(1).ToString());
// Display the index of the first selected item in the ListBox.
System.Diagnostics.Debug.WriteLine((new Integer
(listBox1.get_SelectedIndices().get_Item(0))).ToString());
} //button1_Click
private function button1_Click(sender : Object, e : System.EventArgs) { // Create an instance of the ListBox. var listBox1 : ListBox = new ListBox(); // Set the size and location of the ListBox. listBox1.Size = new System.Drawing.Size(200, 100); listBox1.Location = new System.Drawing.Point(10,10); // Add the ListBox to the form. this.Controls.Add(listBox1); // Set the ListBox to display items in multiple columns. listBox1.MultiColumn = true; // Set the selection mode to multiple and extended. listBox1.SelectionMode = SelectionMode.MultiExtended; // Shutdown the painting of the ListBox as items are added. listBox1.BeginUpdate(); // Loop through and add 50 items to the ListBox. for (var x : int = 1; x <= 50; x++) { listBox1.Items.Add("Item " + x.ToString()); } // Allow the ListBox to repaint and display the new items. listBox1.EndUpdate(); // Select three items from the ListBox. listBox1.SetSelected(1, true); listBox1.SetSelected(3, true); listBox1.SetSelected(5, true); @if(@DEBUG) // Display the second selected item in the ListBox to the console. System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems[1].ToString()); // Display the index of the first selected item in the ListBox. System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices[0].ToString()); @end }
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition , Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework e .NET Compact Framework non supportano tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.