WinForms Issues - Autoquark/dq1-unofficial-mod-editor GitHub Wiki

Intro

I'm using this page to keep track of all the ways in which WinForms is rubbish, so that when I come back later I can see why I did all of the hacky things that I did.

ListBox does not notify when the selection changes due to an item being remove from the backing list

Problem

  1. ListBox.DataSource = new BindingList() { some content... }
  2. bindingList.Remove(listBox.SelectedItem)
  3. listBox.SelectedValue and listBox.SelectedItem change, but neither SelectedValueChanged nor SelectedIndexChanged fire.

This causes issues with (for example) keeping the list of items and the detail view in synch for a master-detail control.

Solutions

Run update code in both SelectedValueChanged and bindingList.ListChanged

When ListBox.SelectedItem is removed from the backing BindingList<>, the ListChanged handler is called before ListBox.SelectedItem changes, so we can't use the new value.