This documentation is archived and is not being maintained.

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.


private void button1_Click(object sender, System.EventArgs e)

    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