Mhd
Mhd

Reputation: 2978

How to sort a Combobox programmatically

In my winforms application, I was looking for a way to add an item (Bitmap object) at the beginning of comboBox after sorting all other items.

I was expecting to see method Sort() for comboBoxes but all what I can find as solution was to turn on/off Sorted feature:

// add all items to mycomboBox
mycomboBox.Sorted = true;
mycomboBox.Sorted = false;
mycomboBox.Insert(0, myItem);

Is there a better option to insert a new item at position 0 of sorted combobox?

Note: I'm using Net Framework 4.0

Upvotes: 0

Views: 2539

Answers (1)

David
David

Reputation: 1871

You can't have both sorting and manual placement.

I would recommend simply adding the items to the combobox pre-sorted, assuming you can add them all at once.

myComboBox.Items.Add(myList.OrderBy(r => r).ToArray());

Upvotes: 1

Related Questions