Generics lists and cloned combobox controls

L

Lonifasiko

Hi,

I'm having quite a strange behaviour when using Generics classes and
ComboBox controls in Winforms applications. Hope somebody has seen the
same behaviour.

Two combobox controls in my form. I want to load them with same data.
I've got a generics list full of drinks (List<Drink>).

If I for example do:

combo1.DataSource = myList; // myList is a List<Drink> estructure
combo1.DisplayMember = "description";
combo1.ValueMember = "code";

combo2.DataSource = myList;
combo1.DisplayMember = "description";
combo1.ValueMember = "code";

No problems, combos are loaded in the correct way. Then I select a
value in first combo. Selected value properly selected. When I select
a value in the second combo, same value gets selected in first combo.

It seems like they would be cloned combos that behave the same way. In
fact, when one item is selected, SelectedIndexChanged method is fired
for both of the combos.

My workaround has been to load items in the combo with a foreach
instruction iterating the list of drinks:

foreach (Drink d in myList)
combo1.Items.Add(d);

Just to let the group know...
 
M

Marc Gravell

From the description, it souds like SelectedIndex is hooking into the
Position of the CurrencyManager... a quick look in reflector confirms
this:

private void DataManager_PositionChanged(object sender, EventArgs e)
{
if ((this.dataManager != null) && this.AllowSelection)
{
this.SelectedIndex = this.dataManager.Position;
}
}

So basically, you need to copy the list first, so that you get
separate CurrencyManagers (and thus separate Positions).

Marc
 
L

Lonifasiko

Hi, thanks for joining the thread I started.

What do you mean by "copy the list first"? Sorry but I don't
understand what you mean.

PS: These behaviour sounds like a little bug in the BCL of .NET
Framework, don't you think?

Regards.
 
M

Marc Gravell

as in: create a second list and put the same contents in the second
list.

As for whether it is a bug, that comes down a little to how you want
currency to work; in most cases this behavior is desirable, as (for
example) moving up and down a grid can toggle various other UI
elements to update with the value for the current row.

Marc
 
L

Lonifasiko

Thanks.

I think it's not a very suitable solution to build another list and
copy elements there to load the second combobox. That will consume
quite a big amount of memory.

PS: I continue betting for being a bug. Don't think that currency
behaviour is desirable for most cases.

Regards.
 
M

Marc Gravell

Remeber: for classes, you aren't copying the object - just the
reference. So yes, there will still be a cost - but it might not be as
big as you think

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top