How do you refresh a combo box item in-place?
.net, c#, combobox, vb.net, winforms
Solution
Got it, using Donut's suggestion.
In form class:
private BindingList<PlantComboBoxItem> _plantList;
In loading method:
_plantList = new BindingList<PlantComboBoxItem>(plantItems);
cboPlants.DataSource = _plantList;
In SelectedIndexChanged event:
int selectedIndex = cboPlants.SelectedIndex;
_plantList.ResetItem(selectedIndex);
Thank you!
Problem
The ComboBox Items collection is an ObjectCollection, so of course you can store anything you want in there, but that means you don't get a Text property like you would with, say, a ListViewItem. The ComboBox displays the items by calling ToString() on each item, or using reflection if the DisplayMember property is set. My ComboBox is in DropDownList mode. I have a situation where I want to refresh the item text of a single item in the list when it gets selected by the user. The problem is that the ComboBox doesn't re-query for the text at any time besides when it loads up, and I can't figure out how else to do what I want besides removing and re-adding the selected item like so: ``` PlantComboBoxItem selectedItem = cboPlants.SelectedItem as PlantComboBoxItem; // ... cboPlants.BeginUpdate(); int selectedIndex = cboPlants.SelectedIndex; cboPlants.Items.RemoveAt(selectedIndex); cboPlants.Items.Insert(selectedIndex, selectedItem); cboPlants.SelectedIndex = selectedIndex; cboPlants.EndUpdate(); ``` This code works fine, except for the fact that my SelectedIndex event ends up getting fired twice (once on the original user event, and then again when I re-set the property in this code). In this case, it's not a big deal that the event is fired twice, but it's inefficient, and I hate that. I could rig up a flag so it exits the event the second time, but that's hacking. Is there a better way to get this to work?