Kelly Cline
Kelly Cline

Reputation: 2246

Winforms Binding to ListBox

I have a Windows Form with a ListBox. The form has this method

public void SetBinding(BindingList<string> _messages)
{
  BindingList<string> toBind = new BindingList<string>( _messages );
  lbMessages.DataSource = toBind;
}

Elsewhere, I have a class called Manager that has this property

public BindingList<string> Messages { get; private set; }

and this line in its constructor

Messages = new BindingList<string>();

Finally, I have my startup program that instantiates the form and the manager and then calls

form.SetBinding(manager.Messages);

What else do I have to do so that a statement in Manager like this:

Messages.Add("blah blah blah...");

will cause a line to be added to and displayed immediately in the form's ListBox?

I don't at all have to do it this way. I just want my Manager class to be able to post to the form while it is doing its job.

Upvotes: 2

Views: 10005

Answers (2)

Angshuman Agarwal
Angshuman Agarwal

Reputation: 4866

Add a new Winforms Project. Drop a ListBox. Excuse the design. Just wanted to show that it works what you want to achieve by using BindingSource and BindingList combo.

Using the BindingSource is the key here

Manager class

public class Manager
{
    /// <summary>
    /// BindingList<T> fires ListChanged event when a new item is added to the list. 
    /// Since BindingSource hooks on to the ListChanged event of BindingList<T> it also is
    /// “aware” of these changes and so the BindingSource fires the ListChanged event. 
    /// This will cause the new row to appear instantly in the List, DataGridView or make any
    /// controls listening to the BindingSource “aware” about this change.
    /// </summary>
    public  BindingList<string> Messages { get; set; }
    private BindingSource _bs;

    private Form1 _form;

    public Manager(Form1 form)
    { 
        // note that Manager initialised with a set of 3 values
        Messages = new BindingList<string> {"2", "3", "4"};

        // initialise the bindingsource with the List - THIS IS THE KEY  
        _bs = new BindingSource {DataSource = Messages};
        _form = form;
    }

    public void UpdateList()
    {
         // pass the BindingSource and NOT the LIST
        _form.SetBinding(_bs);
    }
}

Form1 class

   public Form1()
    {
        mgr = new Manager(this);
        InitializeComponent();

        mgr.UpdateList();
    }

    public void SetBinding(BindingSource _messages)
    {
        lbMessages.DataSource = _messages;

        // NOTE that message is added later & will instantly appear on ListBox
        mgr.Messages.Add("I am added later");
        mgr.Messages.Add("blah, blah, blah");
    }

Upvotes: 1

LarsTech
LarsTech

Reputation: 81610

I think the problem is with your SetBinding method where you are making a new binding list, which means you aren't binding to the list in the Manager object anymore.

Try just passing the current BindingList to the datasource:

public void SetBinding(BindingList<string> messages)
{
  // BindingList<string> toBind = new BindingList<string>(messages);
  lbMessages.DataSource = messages;
}

Upvotes: 3

Related Questions