Export (0) Print
Expand All
Expand Minimize

Code: Searching for an Item in a ListBox Control (Visual C#)

Visual Studio .NET 2003

This example searches for a string in a Windows Forms ListBox control.

Example

private void button1_Click(object sender, System.EventArgs e)
{
    listBox1.Items.Add("One");
    listBox1.Items.Add("Two");
    listBox1.Items.Add("Three");

    if (listBox1.FindString("Two") != -1)
        MessageBox.Show("Found it!");
}

Compiling the Code

This example requires:

  • A form with a ListBox control named listBox1 and a Button control named button1. Set button1's Click event handler to button1_Click.
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.

See Also

Windows Forms Example Topics

Show:
© 2014 Microsoft