I have two ListBoxes and I want to move items between them. The code looks like this:
private void FillItems()
{
allItems = GetAllItems();
availableItems = new List<string>(allItems);
selectedItems = new List<string>();
itemsListBox.DataSource = availableItems;
selectedItemsListBox.DataSource = selectedItems;
}
private void addItemButton_Click(object sender, EventArgs e)
{
var itemsToAdd = itemsListBox.SelectedItems;
foreach (string item in itemsToAdd)
{
availableItems.Remove(item);
selectedItems.Add(item);
}
}
Simple enough. I move strings from one list into another.
Now this doesn't work, just as it shouldn't. I realise that it's missing some way of notifying about collection change. So I tried these options:
Calling
Refresh()
on the list boxes. No avail.Using an extra layer of data binding (
BindingSource
). Also no avail.Calling
ResetBindings()
on the control. Still nothing.
What am I doing wrong here? Should I use some kind of an observable collection?