Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to get selected item OR entered text from combobox

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;
like image 585
Joe Gayetty Avatar asked Mar 14 '23 12:03

Joe Gayetty


1 Answers

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;
like image 139
Joe Gayetty Avatar answered Apr 25 '23 17:04

Joe Gayetty