I have a combobox that I pre-populate with numerous possible choices. But I also want the option open for the user to manually enter text that is not one of the choices. So I leave the DropDownStyle set to DropDown so this is possible.
My question is, what is the most efficient (yet proper) way to write the code to return the value the user either selects, or manually enters?
Currently I am using the following code. But it seems a bit verbose for such a simple task. Is there a better (shorter) way to obtain the same result?
string Code1 = comboBox_Code1.GetItemText(comboBox_Code1.SelectedItem);
if (Code1.Length == 0) Code1 = comboBox_Code1.Text;
Siva Gopal posted the answer in a comment. It is by far the shortest and simplest solution suggested. I have tested it and it works when the user selects a pre-populated value, and it also works when the user manually types in a value!
string Code1 = comboBox_Code1.Text;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With