-1

I have built a form in C# where I want to display the ComboBox selection in a TextBox. I know it sounds very simple but I keep getting errors.

For example, if the user selects "California" from the drop down menu, I want the TextBox to display "California". I have tried the following code for the ComboBox but haven't had any luck. Any help is appreciated!

cmb.SelectedItem = txt1.Text

Or

cmb.Text = txt1.Text
0xCursor
  • 2,242
  • 4
  • 15
  • 33

1 Answers1

0

You could try:

string selected = this.ComboBox.GetItemText(this.ComboBox.SelectedItem);

to get the string of the selected item from the ComboBox. Then you could use:

textbox.Text = selected;

to set the TextBox text.

Check out this post and this post.

0xCursor
  • 2,242
  • 4
  • 15
  • 33
  • You could try `string selected = combobox.Items[combobox.SelectedIndex].ToString();` or `string selected = combobox.Text;`, although, the latter isn't as preferred. – 0xCursor Aug 07 '18 at 21:08