Search code examples
c#winformsevent-driven

If user picks a string on a ComboBox how do you get it to display?


I am trying to get the user's input for picking a colour. I have used a ComboBox.

How do get the program to see what selection the user has made in order to display it in a MessageBox? I was thinking of using an if statement.

string red;
string blue;
string white;
string green;
string purple;
string yellow;
string colour = "";

Solution

  • Use the SelectedIndexChanged event handler.

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
       MessageBox.Show(comboBox1.SelectedItem.ToString());
    }