I am updating my combobox with items, it works fine the first time I open the form but the second, it is setting the values, but they are not getting displayed.
combo.Items.Clear();
int selectedIndex = -1;
foreach(var item in itemstoadd)
{
int index = combo.Items.Add(item.Name + " - " + item.Description);
if (item.Name.Equals(itemToSelect))
{
selectedIndex = index;
}
}
combo.SelectedIndex = selectedIndex;
so after the second time combo.Items has a count of 7 and all values are valid. but then when the method finishes and the form redraws, the Items list has NOT been updated.
I have looked around stackoverflow and cannot find anything that works for me.
it it on the right thread, there is only 1 instance that i can tell. I have tried BeginUpdate() / EndUpdate() Update() Suspend / ResumeLayout
but still nothing will update this items display.
itemtoSelect is passed into this method, and this is called from another combobox event selectedindexchanged, and initially when i setup the display.
I have hit debug points in the code and the items are all being set on the first window opening and the second window opening. I think the only think im not 100% sure is if the combobox is the right combobox, as in if its the older one from the first window opening. I think i can just check this with gethashcode() is there another way to check the reference of an object to confirm if it is the one i think it is?
When you clear Combo.Items
it resets the SelectedIndex. Then, when you make the assignment combo.SelectedIndex = selectedIndex;
, it generates a new SelectedIndexChanged event, even if you set it to the original value. This can cause problems if you have not completed execution of the first SelectedIndexChanged handler call.
i had an event i had NOT deallocated, that was keeping the original combobox alive and keeping the event that i had breakpointed firing on the old combobox.
once i deallocated this event on closing of the window, then next time i create the window and the combobox it all works great.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.