Freigeben über


ListBox.GetSelected-Methode

Gibt einen Wert zurück, der angibt, ob das angegebene Element ausgewählt ist.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Function GetSelected ( _
    index As Integer _
) As Boolean
'Usage
Dim instance As ListBox
Dim index As Integer
Dim returnValue As Boolean

returnValue = instance.GetSelected(index)
public bool GetSelected (
    int index
)
public:
bool GetSelected (
    int index
)
public boolean GetSelected (
    int index
)
public function GetSelected (
    index : int
) : boolean

Parameter

  • index
    Der nullbasierte Index des Elements, der bestimmt, ob dieses ausgewählt ist.

Rückgabewert

true, wenn das angegebene Element derzeit in der ListBox ausgewählt ist, andernfalls false.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentOutOfRangeException

Der index-Parameter ist kleiner als 0 (null) oder größer oder gleich dem Wert der Count-Eigenschaft der ListBox.ObjectCollection-Klasse.

Hinweise

Mit dieser Methode können Sie schnell bestimmen, ob ein angegebenes Element ausgewählt ist. Diese Methode ist nützlich, wenn ein bestimmter Vorgang ausgeführt werden muss, während ein bestimmtes Element in einer ListBox mit Mehrfachauswahl ausgewählt ist.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie mit der GetSelected-Methode die in einer ListBox ausgewählten Elemente bestimmt werden, damit die nicht ausgewählten Elemente ausgewählt werden können und die Auswahl der ausgewählten Elemente aufgehoben werden kann. Außerdem wird veranschaulicht, wie mit der SelectionMode-Eigenschaft für eine ListBox das Auswählen mehrerer Elemente ermöglicht wird. Mithilfe der Sorted-Eigenschaft wird das automatische Sortieren von Elementen in einer ListBox dargestellt. Für dieses Beispiel muss einem Formular die ListBoxlistBox1 hinzugefügt werden, und die im Beispiel definierte InitializeMyListBox-Methode muss aus dem Load-Ereignis des Formulars aufgerufen werden.

Private Sub InitializeMyListBox()
   ' Add items to the ListBox.
   listBox1.Items.Add("A")
   listBox1.Items.Add("C")
   listBox1.Items.Add("E")
   listBox1.Items.Add("F")
   listBox1.Items.Add("G")
   listBox1.Items.Add("D")
   listBox1.Items.Add("B")

   ' Sort all items added previously.
   listBox1.Sorted = True

   ' Set the SelectionMode to select multiple items.
   listBox1.SelectionMode = SelectionMode.MultiExtended

   ' Select three initial items from the list.
   listBox1.SetSelected(0, True)
   listBox1.SetSelected(2, True)
   listBox1.SetSelected(4, True)

   ' Force the ListBox to scroll back to the top of the list.
   listBox1.TopIndex = 0
End Sub

Private Sub InvertMySelection()

   Dim x As Integer
   ' Loop through all items the ListBox.
   For x = 0 To listBox1.Items.Count - 1

      ' Determine if the item is selected.
      If listBox1.GetSelected(x) = True Then
         ' Deselect all items that are selected.
         listBox1.SetSelected(x, False)
      Else
         ' Select all items that are not selected.
         listBox1.SetSelected(x, True)
      End If
   Next x
   ' Force the ListBox to scroll back to the top of the list.
   listBox1.TopIndex = 0
End Sub
private void InitializeMyListBox()
{
   // Add items to the ListBox.
   listBox1.Items.Add("A");
   listBox1.Items.Add("C");
   listBox1.Items.Add("E");
   listBox1.Items.Add("F");
   listBox1.Items.Add("G");
   listBox1.Items.Add("D");
   listBox1.Items.Add("B");

   // Sort all items added previously.
   listBox1.Sorted = true;

   // Set the SelectionMode to select multiple items.
   listBox1.SelectionMode = SelectionMode.MultiExtended;

   // Select three initial items from the list.
   listBox1.SetSelected(0,true);
   listBox1.SetSelected(2,true);
   listBox1.SetSelected(4,true);

   // Force the ListBox to scroll back to the top of the list.
   listBox1.TopIndex=0;
}

private void InvertMySelection()
{
   // Loop through all items the ListBox.
   for (int x = 0; x < listBox1.Items.Count; x++)
   {
      // Determine if the item is selected.
      if(listBox1.GetSelected(x) == true)
         // Deselect all items that are selected.
         listBox1.SetSelected(x,false);      
      else
         // Select all items that are not selected.
         listBox1.SetSelected(x,true);
   }
   // Force the ListBox to scroll back to the top of the list.
   listBox1.TopIndex=0;
}
private:
   void InitializeMyListBox()
   {
      // Add items to the ListBox.
      listBox1->Items->Add( "A" );
      listBox1->Items->Add( "C" );
      listBox1->Items->Add( "E" );
      listBox1->Items->Add( "F" );
      listBox1->Items->Add( "G" );
      listBox1->Items->Add( "D" );
      listBox1->Items->Add( "B" );

      // Sort all items added previously.
      listBox1->Sorted = true;

      // Set the SelectionMode to select multiple items.
      listBox1->SelectionMode = SelectionMode::MultiExtended;

      // Select three initial items from the list.
      listBox1->SetSelected( 0, true );
      listBox1->SetSelected( 2, true );
      listBox1->SetSelected( 4, true );

      // Force the ListBox to scroll back to the top of the list.
      listBox1->TopIndex = 0;
   }

   void InvertMySelection()
   {
      // Loop through all items the ListBox.
      for ( int x = 0; x < listBox1->Items->Count; x++ )
      {
         // Select all items that are not selected,
         // deselect all items that are selected.
         listBox1->SetSelected( x,  !listBox1->GetSelected( x ) );
      }
      listBox1->TopIndex = 0;
   }
private void InitializeMyListBox()
{
    // Add items to the ListBox.
    listBox1.get_Items().Add("A");
    listBox1.get_Items().Add("C");
    listBox1.get_Items().Add("E");
    listBox1.get_Items().Add("F");
    listBox1.get_Items().Add("G");
    listBox1.get_Items().Add("D");
    listBox1.get_Items().Add("B");

    // Sort all items added previously.
    listBox1.set_Sorted(true);

    // Set the SelectionMode to select multiple items.
    listBox1.set_SelectionMode(SelectionMode.MultiExtended);

    // Select three initial items from the list.
    listBox1.SetSelected(0, true);
    listBox1.SetSelected(2, true);
    listBox1.SetSelected(4, true);

    // Force the ListBox to scroll back to the top of the list.
    listBox1.set_TopIndex(0);
} //InitializeMyListBox

private void InvertMySelection()
{
    // Loop through all items the ListBox.
    for (int x = 0; x < listBox1.get_Items().get_Count(); x++) {
        // Determine if the item is selected.
        if (listBox1.GetSelected(x) == true) {
            // Deselect all items that are selected.
            listBox1.SetSelected(x, false);
        }        
        else {
            // Select all items that are not selected.
            listBox1.SetSelected(x, true);
        }
    }
    // Force the ListBox to scroll back to the top of the list.
    listBox1.set_TopIndex(0);
} //InvertMySelection

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

ListBox-Klasse
ListBox-Member
System.Windows.Forms-Namespace