I have a class that look like (simplified) :
public class Profile { private String _name; public String Name { get { return _name; } set { _name = value; } } public override ToString() { return Name; } } |
Then, on a form I have a combobox that contains Profile objetcts :
Profile profile1 = new Profile(); profile1.Name = "John"; comboBox1.Items.Add(profile1); |
My problem is when I update the name of one of my object :
profile1.Name = "Luc"; |
the text shown in the combobox isn't updated. Is there a way to update it without the need to remove then readd the object in the combo
Thanks !

How to update a combobox that contains objects ?
Jmontgomery
With regards to binding your ComboBox to your own objects, I believe there might be a better way.
Most of the new controls will allow you to assign IList based objects to the DataSource member.
In your case, create a ProfileList: IList or if you don't want to implement your own list code, do this ProfileList:List<Profile> and then set comboBox.DataSource = myProfileList.
If you implement an indexer in the Profile class, you can then set comboBox.DisplayMember and combo.ValueMember properties. I haven't tried this so you may need to read up on it for exact implementation details.
Use the BeginUpdate/EndUpdate members if adding lots of data to prevent flicker.
Shane O
I tried these methods too, but without success :-(
I could do that, but Profile is a complexe object with a lot of properties... so I prefere my workaround (deleting it from the combo, then re-adding it) :-)
For your code, I know I can use a boolean, but I was asking if there is a better solution. For the moment, I'm doing something like :
comboProfiles.SelectedIndexChanged -= comboProfiles_SelectedIndexChanged;
(...) comboProfiles.SelectedIndex=... (...)
comboProfiles.SelectedIndexChanged +=
new System.EventHandler(comboProfiles_SelectedIndexChanged);But maybe there are better solutions that I don't know.
BChristensen
int index = comboProfiles.SelectedIndex;
comboProfiles.Items.RemoveAt(index);
comboProfiles.Items.Insert(index, currentProfile);
comboProfiles.SelectedIndex = index;
But the problem is that the SelectedIndexChanged event is fired, what I don't want...
Edit : another question : is there a way to know if an event (SelectdIndexChanged...) is fired by the user (by changing manually the selected item) or by the code (comb.SelectedIndex=1)
glentrino2duo
I just tested it and you're right it won't update at all. In addition, I tested using combobox1.Invalidate(), .Refresh(), and .Update() without having any luck. Does anyone at Microsoft know what call has to be made to get the combobox to look at what it is displaying and refresh the output
One thing I did that will get around the problem you are experiencing is to create the object again, maintaining all the properties you need, but replacing the value you want to change.
i.e.
int index = combobox1.SelectedIndex;
combobox1.Items[index] = new Profile("Luc");
When I replaced the old object with a new one, it changed in the combobox. However the selectedIndex_Changed event still gets fired. One thing I did was to put a module level boolean that I set to false before changing the item and then set it to false afterwards.
i.e.
public class myClass
{
bool doComboChanged = true;
private void changeText()
{
int index = combobox1.SelectedIndex;
doComboChanged = false;
combobox1.Items[index] = new Profile("Luc");
doComboChanged = true;
}
private void combobox1_SelectedIndexChanged(...)
{
if (doComboChanged)
{
//code to execute goes here
}
}
}
It's a little convoluted, but it works. I'm not aware of any other means to check if your code fired the event or if a response to a user interface did it.
Hope this helps.
123666
Hence, the code would look similar to:
Profile prof = (Profile) combobox1.Items[0];
prof.Name = "Luc";
where 0 is the index of the item you want to modify.
Let me know if this doesn't work for you.