Problem when binding combobox and listbox to a list box (VB.NET)

I am trying to link ComboBox and ListBox to List (Of String) in Vb.Net (VS2013), this is for WinForms application, the point is that after setting DataSource property on ComboBox and ListBox, selecting one item on one of them affects the other item controls, for example, after the controls have been filled with information, if I select an item from the ListBox, then the same item will be selected in the ComboBox, and it will be the same for the ComboBox, if I select an item from it, then this the item will also be selected in the ListBox, so my question is ... how can I bind the dropdown and listbox to the same list (Of String) without affecting the behavior on the controls, the goal is to keep all controls in this form synchronized based on the contents of the list, I declared List in the module like this:

Public listaAreas As New List(Of String)

      

then the controls are populated like this:

    cmbArea.DataSource = listaAreas
    lstAreas.DataSource = listaAreas

      

And I run this method when I need to update the information:

Private Sub RefreshLists()
    lstAreas.DataSource = Nothing
    lstAreas.DataSource = listaAreas

    cmbArea.DataSource = Nothing
    cmbArea.DataSource = listaAreas
End Sub

      

Please let me know if I am missing information, this is my first post, but I think it is clear enough for you to understand what I am trying to do here ... =)

Thanks in advance!

+3


source to share


1 answer


Try setting up separate BindingSources and try using BindingList (Of String) instead of a simple list that won't report element changes:

Private listaAreas As New BindingList(Of String)
Private cbSource As New BindingSource(listaAreas, String.Empty)
Private lbSource As New BindingSource(listaAreas, String.Empty)

Public Sub New()
  InitializeComponent()
  cmbArea.DataSource = cbSource
  lstAreas.DataSource = lbSource
End Sub

      



Your code uses the same currency position, but by defining two separate anchor sources, each will have its own position property.

0


source







All Articles