Ran
Ran

Reputation: 534

C# Binding: How can I disable the CurrencyManager in BindingList so Current Item position is not maintained and not signaled?

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.

Upvotes: 2

Views: 4123

Answers (3)

jyoung
jyoung

Reputation: 5161

Add this line to Form_Load:

this.listBox1.BindingContext = new BindingContext();

Upvotes: 7

Ran
Ran

Reputation: 534

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."

Upvotes: 0

tamberg
tamberg

Reputation: 2017

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

Upvotes: 2

Related Questions