ComboBox refresh display item

  • Thread starter Thread starter Nick Hoggard
  • Start date Start date
N

Nick Hoggard

Hi All,

Really silly question, but how do I get a combo box to refresh the display
field if I update the object that it is based on.

For example, I am populating my combobox by adding instances of a custom
object. That custom object has a ToString property that the combobox uses
as the display field. If I change a property on the custom object then the
ToString value also changes, but the combobox doesn't get refreshed.

Is there something I can call to make the combobox refresh the display
fields, or do I have to remove then add the item again?

Cheers

Nick Hoggard
 
If combobox is not bound (.DataSource == null ), then your best bet is:
combobox1.Items. = combobox1.Items.;

It will force the refresh of the item. It will also reset the selected
index, so you may want to preserve that.

If the combobox is bound to say, ArrayList, it is somewhat more complicated.
Basically, you wil either have to get the CurrencyManager via Reflection and
call Refresh() on it, or implement your own collection that implements
IBindingList and forces bound control to reset on every change. Let me
know, if you need a sample
 
Thanks Alex,
If combobox is not bound (.DataSource == null ), then your best bet is:
combobox1.Items. = combobox1.Items.;


Cool, hadn't thought of that approach. Easier than removing then adding
again at same index.
It will force the refresh of the item. It will also reset the selected
index, so you may want to preserve that.

If the combobox is bound to say, ArrayList, it is somewhat more complicated.
Basically, you wil either have to get the CurrencyManager via Reflection and
call Refresh() on it, or implement your own collection that implements
IBindingList and forces bound control to reset on every change. Let me
know, if you need a sample

I was binding to an arraylist, but have found it's easier just to loop the
array list and add the item to the combo. I may need to try the Reflection
approach though if I find the ArrayList is needed elsewhere in the app.

Thanks again.

Nick
 
Back
Top