views:

113

answers:

2

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);
   }
}
+4  A: 

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

Lucero
Thanks, that works as I was expecting it.
Andy Stampor
Yes, because BindingList<T> supports change notifications (in contrast to List<T> etc.).
Lucero
Check that selection is also maintained correctly. At least dropdownlistbox:es have a bug that they each require separate list as datasource to work correctly, otherwise they will synchronize among themselves in weird ways.
Pasi Savolainen
A: 

Use a bindingSource and bind both listBoxes to that.

Jeff Hall