ListBox Control (Visual C#)
Code: Determining the Selected Item in a ListBox Control (Visual C#)
Visual Studio .NET 2003
This example determines which item has been selected in a Windows Forms ListBox control.
Example:
private void Form1_Load(object sender, System.EventArgs e)
{
listBox1.Items.Add("One");
listBox1.Items.Add("Two");
listBox1.Items.Add("Three");
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if ((string)listBox1.SelectedItem == "Two")
MessageBox.Show((string)listBox1.SelectedItem);
}
Compiling the Code
This example requires:
• A form named Form1 with a ListBox control named listBox1. Set Form1's Load event handler to Form1_Load. Set listBox1's SelectedIndexChanged event handler to listBox1_SelectedIndexChanged.
Note This can also be used with a ComboBox control by substituting a ComboBox control named comboBox1 for the ListBox control and changing the code from listBox1 to comboBox1.
No comments:
Post a Comment