Reputation: 437
I have a ComboBox that can display a long list of items. There is a function on the form that needs to change the display text of these items without actually adding or removing any. There is no data binding going on, I'm accessing the Items list directly.
The problem is, if I modify any property of the item in the Items list, it doesn't update the text in the combo box. There is a button that can move an item from the ComboBox to another adjacent ComboBox, and when it gets moved it displays properly, so the issue seems to be it's just not redrawing it.
I've tried calling Invalidate(), Refresh(), Update(), but it doesn't work. I suppose I could clear the ComboBox and readd everything, but this box can hold thousands of Items so I'm not sure if that's an efficient way of doing it.
Upvotes: 0
Views: 760
Reputation: 421
You need to do the following steps:
here is a sample code: (assuming your items are of string type)
int itemIndex = comboBox1.Items.IndexOf("yourItem");
string itemText = comboBox1.Items[itemIndex].ToString();
itemText = "yourNewString";
comboBox1.Items.Insert(itemIndex, itemText);
if the items are of other than string type then try the following:
int itemIndex = comboBox1.Items.IndexOf(/*your item*/);
var item = comboBox1.Items[itemIndex];
//type cast back to the original type
//change the display text i.e. by changing the display member inside your object
comboBox1.Items.Insert(itemIndex, item);
You may loop over this code snippet if your function does it for multiple items
Upvotes: 0
Reputation: 32667
It seems that it is not possible to update the item labels without databinding. So the only thing left is to remove the according item (Items.RemoveAt) and insert it again (Items.Insert).
Upvotes: 0
Reputation: 216353
Please try simply setting the combobox text property:
combobox1.Text = "newText";
of course you still need to change the item as before.
Upvotes: 1