tags:

views:

661

answers:

3

We have a custom collection of objects that we bind to a listbox control. When an item is added to the list the item appears in the listbox, however when one selects the item the currency manager position will not go to the position. Instead the currency manager position stays at the existing position. The listbox item is high lighted as long as the mouse is press however the cm never changes position.

If I copy one of the collection objects the listbox operates properly.

One additional note the collection also has collections within it, not sure if this would be an issue.

A: 

Collections don't have a sense of "current item". Perhaps your custom collection does, but the ListBox is not using that. It has its own "current item" index into the collection. You need to handle SelectedIndexChanged events to keep them in sync.

Joel B Fant
A: 

You might need to post some code; the following (with two lists tied together only by the CM) shows that it works fine... so to find the bug we might need some code.

using System;
using System.ComponentModel;
using System.Windows.Forms;
static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        BindingList<Foo> foos = new BindingList<Foo>();
        foos.Add(new Foo("abc"));
        foos.Add(new Foo("def"));

        ListBox lb1 = new ListBox(), lb2 = new ListBox();
        lb1.DataSource = lb2.DataSource = foos;
        lb1.DisplayMember = lb2.DisplayMember = "Bar";
        lb1.Dock = DockStyle.Left;
        lb2.Dock = DockStyle.Right;

        Button b = new Button();
        b.Text = "Add";
        b.Dock = DockStyle.Top;
        b.Click += delegate
        {
            foos.Add(new Foo("new item"));
        };
        Form form = new Form();
        form.Controls.Add(lb1);
        form.Controls.Add(lb2);
        form.Controls.Add(b);
        Application.Run(form);
    }


}
class Foo
{
    public Foo(string bar) {this.Bar = bar;}
    private string bar;
    public string Bar {
        get {return bar;}
        set {bar = value;}
    }
}
Marc Gravell
+1  A: 

I found the issue, after spending way too much time....

This issue was related to one of the propertys of the item(custom class) in the collection which was bound to a date picker control. The constructor for the class never set the value to a default value.

This caused an issue with the currency manager not allowing the position to change as the specific property (bound to the date picker) was not valid.

Me bad! I know better!

Murray Van Wieringen
Thanks for the prompt response, this site rocks!
Murray Van Wieringen