IList items "keeping position"

G

Giovanni Bassi

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
 
J

Jay B. Harlow [MVP - Outlook]

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.CopyTo
m_list.CopyTo(array, index)
End Sub

Public ReadOnly Property Count() As Integer _
Implements ICollection.Count
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.DataSource = New ListView(list)
Me.ComboBox1.DataSource = New ListView(list)

End Sub


Hope this helps
Jay
 
G

Giovanni Bassi

Hello Jay,

Thanks for giving me some light on this subject. I really appreciate your
help.
Regarding your demonstration on how to extend the ArrayList, I had already
done that. Still the problem would occur. Your explanation helped very much,
though, because you reminded me of the BindingContext, which was totally
forgot by me...
I have simply added two simple lines where I create a BindingContext object
and another one where I set the ComboBox's BindingContext Property to this
new object and no more problems. The objects are now independent.

Thanks again!

Giovanni Bassi
 
J

Jay B. Harlow [MVP - Outlook]

Giovanni,
In case you did not try it. My sample of just setting the DataSource to
distinct ListView objects:

Keeps the two data sources separate without getting into the specifics of a
BindingContext hidden, while allowing them to share a common actual
ArrayList.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top