Andy Stampor
Andy Stampor

Reputation: 688

Multiple Listboxes bound to same List

I am trying to bind several ListBoxs to a List. When a ListBox on one form is updated, I want it to update the other ListBox, too.

The problem I am running into is that it doesn't seem to update the view on the ListBox when I update the underlying List. If I look at the ListBox.Items in debug, I can see that all the items I add are there, but are not being displayed. Additionally, when I open another form that displays the List on a ListBox, it does correctly display whatever items had already been added.

private List<String> _list;

public Form1()
{
   InitializeComponent();

   _list = StaticInstanceOfList.GetInstance();
   listbox1.DataSource = _list;
}

public void AddStringToList(string value)
{
   if (!_list.Contains(value))
   {
      _list.Add(value);
   }
}

Upvotes: 0

Views: 446

Answers (2)

Jeff Hall
Jeff Hall

Reputation: 1754

Use a bindingSource and bind both listBoxes to that.

Upvotes: 0

Lucero
Lucero

Reputation: 60246

Try to use a BindingList<T> to store your items and then assign this list to both listboxes via the DataSource property.

Upvotes: 5

Related Questions