Export (0) Print
Expand All

ListBox.SelectedItem Property

Gets or sets the currently selected item in the ListBox.

[Visual Basic]
Public Property SelectedItem As Object
[C#]
public object SelectedItem {get; set;}
[C++]
public: __property Object* get_SelectedItem();
public: __property void set_SelectedItem(Object*);
[JScript]
public function get SelectedItem() : Object;
public function set SelectedItem(Object);

Property Value

An object that represents the current selection in the control.

Remarks

For a standard ListBox, you can use this property to determine which item is selected in the ListBox. If the SelectionMode property of the ListBox is set to either SelectionMode.MultiSimple or SelectionMode.MultiExtended (which indicates a multiple-selection ListBox) and multiple items are selected in the list, this property can return any selected item.

To retrieve a collection containing all selected items in a multiple-selection ListBox, use the SelectedItems property. If you want to obtain the index position of the currently selected item in the ListBox, use the SelectedIndex property. In addition, you can use the SelectedIndices property to obtain all the selected indexes in a multiple-selection ListBox.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the SelectedIndexChanged event to search for and select an items in a different ListBox control. The example uses the SelectedIndexChanged event to determine when the selected item in the ListBox is changed. The example code then reads the text of the item using the SelectedItem property and calls the FindString method on a different ListBox using the text returned by SelectedItem in the first ListBox. If an item is found in the other ListBox, the item is selected. This example assumes that two ListBox controls, named listBox1 and listBox2, have been added to a form and that both ListBox controls contain items that are identical. The example also assumes that the event-handling method defined in the example is connected to the SelectedIndexChanged event of listBox1.

[Visual Basic] 
Private Sub listBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
   ' Get the currently selected item in the ListBox.
   Dim curItem As String = listBox1.SelectedItem.ToString()

   ' Find the string in ListBox2.
   Dim index As Integer = listBox2.FindString(curItem)
   ' If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2.
   If index = -1 Then
      MessageBox.Show("Item is not available in ListBox2")
   Else
      listBox2.SetSelected(index, True)
   End If
End Sub

[C#] 
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   // Get the currently selected item in the ListBox.
   string curItem = listBox1.SelectedItem.ToString();

   // Find the string in ListBox2.
   int index = listBox2.FindString(curItem);
   // If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2.
   if(index == -1)
      MessageBox.Show("Item is not available in ListBox2");
   else
      listBox2.SetSelected(index,true);
}

[C++] 
private:
   void listBox1_SelectedIndexChanged(Object* /*sender*/, System::EventArgs* /*e*/)
   {
      // Get the currently selected item in the ListBox.
      String* curItem = listBox1->SelectedItem->ToString();

      // Find the string in ListBox2.
      int index = listBox2->FindString(curItem);
      // If the item was not found in ListBox 2 display a message box,
      //  otherwise select it in ListBox2.
      if(index == -1)
         MessageBox::Show(S"Item is not available in ListBox2");
      else
         listBox2->SetSelected(index,true);
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

ListBox Class | ListBox Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft