Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set comboBox to custom display format

I've got a WinForms comboBox that contains a list of "Regions" (a custom class I've set up). Each Region has properties Name, Abbreviation, etc. I know I can set the comboBox to comboBox.DisplayMember = "Name";, but I want the display formatting to be "(" + Abbreviation + ") " + Name (e.g. (OR) Oregon).

I know I could create a separate property for this (e.g. DisplayName) and just set the comboBox.DisplayMember = "DisplayName"; but is there another way to do it? Something like comboBox.DisplayMember = "(" + Abbreviation + ") " + Name; or whatever?

like image 943
derekantrican Avatar asked Sep 05 '25 21:09

derekantrican


1 Answers

You can use combobox's Format event.

 private void comboBox1_Format(object sender, ListControlConvertEventArgs e)
    {
        string Name = ((yourClass)e.ListItem).Property1;
        string LastName = ((yourClass)e.ListItem).Property2;
        e.Value = Name + " " + LastName;
    }
like image 185
Berkay Yaylacı Avatar answered Sep 10 '25 02:09

Berkay Yaylacı