Giovanni,
Oh! the joys of databinding. :-)
When you bind the first ListBox to the list, an entry is made in the forms
BindingContext property, when you bind the ComboBox to the same list, the
form sees the entry in the BindingContext and reuses it.
The easiest way to fix this is to have two lists.
Without physically cloning the list an easy way to have two lists is to
introduce a 'View' class. A 'View' class is a class that implements IList or
IBindingList that holds a reference to your original IList, delegating all
the IList methods to this contained list. Similar to the relationship
between DataTable & DataView, a DataTable can have multiple independent
DataView objects referring to it. When you bind to a DataTable you are
actually binding to a DataView object. By implementing IBindingList and
other advanced data binding interfaces, you can give simple collection such
as ArrayList rich data binding abilities...
Something like:
Public Class ListView
Implements IList
Private ReadOnly m_list As IList
Public Sub New(ByVal list As IList)
m_list = list
End Sub
Public Sub CopyTo(ByVal array As Array, ByVal index As Integer) _
Implements ICollection.Cop yTo
m_list.CopyTo(a rray, index)
End Sub
Public ReadOnly Property Count() As Integer _
Implements ICollection.Cou nt
Get
Return m_list.Count
End Get
End Property
...
' all other IList methods delegate to m_list as above
End Class
Then you would bind to ListView objects:
Public Class MainForm
Private Sub MainForm_Load
Dim list As New ArrayList
Me.ListBox1.Dat aSource = New ListView(list)
Me.ComboBox1.Da taSource = New ListView(list)
End Sub
Hope this helps
Jay
"Giovanni Bassi" <gb****@coair.c om> wrote in message
news:eW******** ******@TK2MSFTN GP11.phx.gbl...
Hello All,
I have a class implementing IList and a global object of this class type.
I have two different form objects. One has a ListBox and the other has
ComboBox. Both of their datasources are set to this global object.
The problem is that when I select an item in the ListBox the combobox
selecteditem changes to the same item selected in the ListBox. And the
oposite is also true, so when I select an item in the ComboBox my ListBox
SelectedItem changes.
I have no idea why this is happening. How can I prevent this from
happening?
Thanks in advance for all the replies,
Giovanni Bassi