-4

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 = "";
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • 1
    In the title, you said ListBox, and then in the question, it's ComboBox. Which one are you using ? – Kaj Mar 04 '20 at 14:10
  • 1
    what not use a color picker instead? – Ctznkane525 Mar 04 '20 at 14:12
  • 3
    what GUI technology are you using? WPF, Winforms ect... ? Usually a control with selection has a `SelectedIndexChanged` event which will be fired, when the user chooses something. And the control will give you the value directly there is no need for any if statements. – Mong Zhu Mar 04 '20 at 14:17
  • please show us the code which populates the listbox or combobox. – Mong Zhu Mar 04 '20 at 14:18
  • Answers will be different for WPF and WinForms. Please add on which you are working on. WinForms example: [SelectedIndexChanged event](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.selectedindexchanged?view=netframework-4.8), in WPF you'd rather work with binding. – Fildor Mar 04 '20 at 14:20

1 Answers1

2

Use the SelectedIndexChanged event handler.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   MessageBox.Show(comboBox1.SelectedItem.ToString());
}
B Minster
  • 331
  • 3
  • 16