Changing the selection of the ListBox changes the other selection of the ListBox. What's happening?
I have a Windows Forms Application with two ListBox controls on the same form. They both have their own SelectionMode for "MultiExtended".
When I change the selection of one, select the other changes.
Now I thought I did something stupid with my SelectedIndexChanged handlers, so I deleted them and rewrote them from scratch and you have a problem.
So, I created a completely new WinForms application and dragged two ListBoxes onto the form panel.
In the constructor, I filled them both with the following.
List<Thing> data = new List<Thing>();
for ( int i = 0; i < 50; i++ ) {
Thing temp = new Thing();
temp.Letters = "abc " + i.ToString();
temp.Id = i;
data.Add(temp);
}
listBox1.DataSource = data;
listBox1.DisplayMember = "Letters";
listBox1.ValueMember = "Id";
List<Thing> data2 = new List<Thing>();
for ( int i = 0; i < 50; i++ ) {
Thing temp = new Thing();
temp.Letters = "abc " + i.ToString();
temp.Id = i;
data2.Add(temp);
}
listBox2.DataSource = data2;
listBox2.DisplayMember = "Letters";
listBox2.ValueMember = "Id";
And then I created and ran the application.
Selecting some values will begin to see if symptoms were present. And they were!
This is literally all the code I added to the form, I didn't add any event handlers, I tried it with the SelectionMode parameter set to "One" and "MultiExtended".
Can anyone give me a clue why this is happening.
Greetings
source to share
This is not a list that stores the current position - this is CurrencyManager
. Any controls (with the same BindingContext
) with the same link as DataSource
will have access to CurrencyManager
. By using different instances of lists, you get different instances CurrencyManager
and therefore a separate item.
You can achieve the same simply by using .ToList()
, or creating a new one List<T>
with the same content (as per your original post), or by assigning a new BindingContext
one to one of the controls:
control.BindingContext = new BindingContext();
source to share
And further...
Finally I got it from him.
I have bound two ListBoxes to the same list. changing the code to
theListBox.DataSource = _contacts.Take(_contacts.Count).ToList();
the problem has been fixed.
It seems that the reference to the list it stores also penalizes any binding or selection information for the other ListBox.
Be careful.;)
source to share
I created a new win forms app with two lists on it. They behave as I expected. Can you post the complete code? Here is my code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private class Thing
{
public String Letters { get; set; }
public Int32 Id { get; set; }
}
private void Form1_Load(object sender, EventArgs e)
{
List<Thing> data = new List<Thing>();
for (int i = 0; i < 50; i++)
{
Thing temp = new Thing();
temp.Letters = "abc " + i.ToString();
temp.Id = i;
data.Add(temp);
}
listBox1.DataSource = data;
listBox1.DisplayMember = "Letters";
listBox1.ValueMember = "Id";
List<Thing> data2 = new List<Thing>();
for (int i = 0; i < 50; i++)
{
Thing temp = new Thing();
temp.Letters = "abc " + i.ToString();
temp.Id = i;
data2.Add(temp);
}
listBox2.DataSource = data2;
listBox2.DisplayMember = "Letters";
listBox2.ValueMember = "Id";
}
}
source to share