Bound BoxWhile working on a recent application in C#, I ran into a situation I hadn’t hit before and worked out a solution I wanted to pass along for others that might come up against this same scenario. I’m not sure if it’s the “preferred” way to do it, but it sure worked for me and will definitely work for you. In C#, comboboxes for Windows Forms only allows you to set the display text for items in a combobox. However, in the situation I ran into, I needed a way to control the display value as well as the hidden values of a combobox. For example, in HTML, you can create a select box that includes options to choose from which allow you to control the value that gets displayed to the user as well as the value that is actually passed from the user interface to the server. You can do that like so:

<select name=”cmbComboBox”>
<option value=”1″>One</option>
<option value=”2″>Two</option>
<option value=”3″>Three</option>
</select>

So, I wanted to do the same thing using C# and a Windows Forms application. The solution I came up with was to create a new object that allowed me to control what the display and hidden values were. By doing this, it also provided me the ability to use existing objects I already had lying around in my app such as domain objects that modeled tables in my database. Since the text attribute is what is expected to be set for each item in a combobox, the only requirement my new object needed was an override on the “ToString” method. Here is what my combobox item class looked like:

    public class ComboBoxItem
    {
        private int _id;
        private string _display;

        public ComboBoxItem(int id, string display)
        {
            this._id = id;
            this._display = display;
        }

        public string Text
        {
            get { return this._display; }
            set { this._display = value; }
        }

        public int ID
        {
            get { return this._id; }
            set { this._id = value; }
        }

        public override string ToString()
        {
            return this._display;
        }
    }

Now, I can create objects of type ComboBoxItem and add them to the items list of my combobox like this:

ComboBoxItem one = new ComboBoxItem(1, “One”);
ComboBoxItem two = new ComboBoxItem(2, “Two”);
ComboBoxItem three = new ComboBoxItem(3, “Three”);

cmbComboBox.Items.Add(one);
cmbComboBox.Items.Add(two);
cmbComboBox.Items.Add(three);

To retrieve the objects from my combobox, all I had to do was get the selected item and cast it to type ComboBoxItem like so:

ComboBoxItem selected = (ComboBoxItem)cmbComboBox.SelectedItem;

With that, I could use the standard getters in my combobox item to get anything inside it. If I had other fields inside my combobox class and wanted those to be displayed in the combobox list, I could concatenate those fields in the “ToString” method.

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.
$2.00
$5.00
Other

Related Posts

Tagged with:  

Leave a Reply