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

+1


source to share


7 replies


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();

      

+4


source


I believe your two controls are sharing CurrencyManager. I'm not sure why exactly. As a workaround, you can simply fill your lists with simple strings. Or you can try creating separate instances of the BindingSource component and bind them to them.



+2


source


I experienced the same when using the same data source in both lists, but creating two equal data sources solved this problem for me. I don't see anything wrong with your code. Something special about the Thing class? Or does it just contain two elements, letters and identifiers?

+1


source


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.;)

+1


source


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";
}

      

}

0


source


I tried to duplicate this behavior on my own machine and was unable to do this run with just the code you provided.

0


source


I'm improving.

On another VS instance, in a completely new solution, this works as expected.

God only knows what I did to make him do what he does

0


source







All Articles