Windows Develop Bookmark and Share   
 index > Windows Forms General > How to know which item in ListBox is slected?
• Windows Forms General
• Windows Forms Designer
• ClickOnce and Setup & Deployment Projects
• Windows Forms Data Controls and Databinding
• Windows Forms Sample Applications

How to know which item in ListBox is slected?

thak you
vincent90152900  Monday, October 23, 2006 6:51 AM

Hi,

accessing ListBox's SelectedItem will return the currently selected item. There's also SelectedIndex, which will return the list index of currently selected item. If it returns -1, no item is selected.

Andrej

Andrej Tozon  Monday, October 23, 2006 7:05 AM

You can use the ListBox.SelectedItem property to retrieve the selected item. This is null when no item was selected. Here is a little example:


private void button1_Click(object sender, EventArgs e)
{
object selectedItem = listBox1.SelectedItem;
if (selectedItem != null)
{
MessageBox.Show(this, "The selected item was: " + selectedItem);
}
else
{
MessageBox.Show(this, "Please select an item.");
}
}

Pieter Joost van de Sande  Monday, October 23, 2006 7:07 AM

Hi,

accessing ListBox's SelectedItem will return the currently selected item. There's also SelectedIndex, which will return the list index of currently selected item. If it returns -1, no item is selected.

Andrej

Andrej Tozon  Monday, October 23, 2006 7:05 AM

ListBox1.SelectedItem.ToString() this will return the selected item in the list box.

Prasant Swain  Monday, October 23, 2006 7:06 AM

You can use the ListBox.SelectedItem property to retrieve the selected item. This is null when no item was selected. Here is a little example:


private void button1_Click(object sender, EventArgs e)
{
object selectedItem = listBox1.SelectedItem;
if (selectedItem != null)
{
MessageBox.Show(this, "The selected item was: " + selectedItem);
}
else
{
MessageBox.Show(this, "Please select an item.");
}
}

Pieter Joost van de Sande  Monday, October 23, 2006 7:07 AM

you may use the ListBox.SelectedItem to retreive the currently selected item.

//Jocke

JockeOlsson  Monday, October 23, 2006 7:08 AM

You can use google to search for other answers

 

More Threads

• VS2008 Winforms Tab Control
• How to register a winforms control as ActiveX control?
• Undo Dataset/Datatable in memory
• SplitContainer panel collection
• how to add combobox selectedValue
• Security Question...
• DATAGRID PRINT
• Visual Studio 2008 on target framework 2.0 Issue
• Controlling process of multiple application instances of same app
• Displaying a Word document into a RitchTextBox