views:

1647

answers:

3

Hi,

I've got two ListBox'es that are databound to the same BindingList.

The issue is that when changing the selected item from the GUI it's changing the position in the BindingList and then the BindingList signals the other ListBox to change its selected item.

So I've got the two ListBoxes Selected Item also synchronized which is not good for me.

I'd like to maintain the list of items in sync. without the cursor position.

How do I disable that cursor so it's not maintained?

sample code (just add two ListBoxes to the Form at design time and register the SelectedIndexChanged events and register the button click event with a button):

public partial class Form1 : Form
{
    BindingList<string> list = new BindingList<string>();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        list.Add("bla1");
        list.Add("bla2");
        list.Add("bla3");

        this.listBox1.DataSource = list;
        this.listBox2.DataSource = list;
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex != -1)
            System.Diagnostics.Trace.WriteLine("ListBox1: " + listBox1.SelectedItem.ToString());
    }

    private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox2.SelectedIndex != -1)
            System.Diagnostics.Trace.WriteLine("ListBox2: " + listBox2.SelectedItem.ToString());
    }

    // Register this event to a button
    private void button1_Click(object sender, EventArgs e)
    {
        list.Add("Test");
    }
}

Thanks, --Ran.

+2  A: 

Declaring listBox1 and listBox2 to be of the following type seems to result in the desired behaviour.

class MyListBox: ListBox {

 protected override void OnSelectedIndexChanged (EventArgs a) {
  if (DataManager != null) {
   DataManager.SuspendBinding();
  }
 }

}

Regards, tamberg

tamberg
Impressive, Thanks.
Ran
Though I've found some issue...If you add an item to the ListBinding then it breaks this solution and it returns to operate like it's without this override.I've added it to the sample code so you can see it.Thanks.
Ran
Hi, there might be another OnXChanged method you have to override. If that does not help, a different approach could involve wrapping the CurrencyManager but right now I do not see a way to change the CurrencyManager instance. Regards
tamberg
I've tried other ways to figure this out and nothing seem to work. So I'll have to stop using the BindingList and just maintain it myself with a regular List. Thanks for all the help,Pardon me for removing it as the selected answer, I don't want to unintentionally mislead other developers.
Ran
A: 

My solution for this issue is to use a normal List instead of the BindingList and just call (before the change) on the Form object: this.BindingContext[Your List].SuspendBinding(); and after the change to the List this.BindingContext[Your List].ResumeBinding(); This updates all the bounded controls.

Notice it's also noted in the MSDN link here:

"If you are bound to a data source that does not implement the IBindingList interface, such as an ArrayList, the bound control's data will not be updated when the data source is updated. For example, if you have a combo box bound to an ArrayList and data is added to the ArrayList, these new items will not appear in the combo box. However, you can force the combo box to be updated by calling the SuspendBinding and ResumeBinding methods on the instance of the BindingContext class to which the control is bound."

Ran
+4  A: 

Add this line to Form_Load:

this.listBox1.BindingContext = new BindingContext();

jyoung
Wow, nice and simple, Thanks!
Ran