Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combo Box if statement

I am having a problem with a combo box here. What I did is made a combo box, added items using comboBox1.Items.Add("Something");. Now I made a text box down there and what I need is when I select something from the combo box the text box changes according to selected item on combo box. What I thought it would do is

if(comboBox1.SelectedItem.ToString() == "Something")
{
    textBox1.Text = "Something";
}

But it's not working for some reason, I tried both without ToString() and with still it is not working.

like image 720
Vulegend Avatar asked Jun 23 '12 07:06

Vulegend


3 Answers

Try using: comboBox1.SelectedText

if(comboBox1.SelectedText == "Something")
{
      textBox1.Text = "Something";
}
like image 175
Darren Avatar answered Sep 28 '22 22:09

Darren


Double click on your combobox and it will generate event for you(SelectedIndexChanged by default). put your code inside that generated event. When you change combobox selected value then you can see the text box value change accordingly.

if you need to show combobox selected value in the textbox, you can put below code inside generated event

textBox1.Text = comboBox1.SelectedItem.ToString();
like image 31
Damith Avatar answered Sep 28 '22 23:09

Damith


Oh found the problem . I was putting the code in wrong section (on textBOx_click) section :P

like image 43
Vulegend Avatar answered Sep 28 '22 21:09

Vulegend