0

I linked a list of object to a ListBox control and I want to make the item switch from a ListBox to an another one with the press of a button :

ListBox3.DataSource = testsToRunList
ListBox3.DisplayMember = "Name"
ListBox3.ValueMember = "ID"

ListBox4.DataSource = testsToIgnoreList
ListBox4.DisplayMember = "Name"
ListBox4.ValueMember = "ID"

...

Private Sub SendDownButton_Click(sender As Object, e As EventArgs) Handles SendDownButton.Click
  Dim myobj = ListBox3.SelectedItem

  testsToRunList.Remove(myobj)
  testsToIgnoreList.Add(myobj)

  ' ListBox3.Refresh()
  ' ListBox4.Refresh()
End Sub

Changing the item in the linked list doesn't affect the content of the ListBox control. How can I make sure that my ListBoxe's content reflects what is in my linked lists?

InteXX
  • 6,135
  • 6
  • 43
  • 80
ESD
  • 675
  • 2
  • 12
  • 35
  • You commented the `ListBox.Refresh()` was it because it wasn't working? – Mederic May 11 '17 at 15:45
  • it's something I tried thinking I had to manually refresh the ListBox, but it didn't have any effect... – ESD May 11 '17 at 15:46
  • I'm thinking maybe it's to your linking your using the `DisplayMember = "Name"` and the `ValueMember = "ID"` so maybe your not removing anything since your not passing the good value. did you try to check what `SelectedItem` returns? – Mederic May 11 '17 at 15:51
  • It returns an object of type Test which is what my lists contain. I can add and remove the object from my list with no problem, but the changes are not reflected on the ListBox even with the DataSource pointing to the list. – ESD May 11 '17 at 15:53
  • 1
    [Connect List to a ListBox and see changes of data source in ListBox](http://stackoverflow.com/q/33623991/3110834) – Reza Aghaei May 11 '17 at 17:30

1 Answers1

2

The List(Of T) class doesn't raise the events required by data-binding to update the control when the list is modified. Either bind via a BindingSource and call the appropriate method to reset the bound control or use a BindingList(Of T) instead.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46