By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,573 Members | 1,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,573 IT Pros & Developers. It's quick & easy.

IList items "keeping position"

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

"Giovanni Bassi" <gb****@coair.com> wrote in message
news:eW**************@TK2MSFTNGP11.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

Nov 20 '05 #2

P: n/a
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
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:uH**************@TK2MSFTNGP10.phx.gbl...
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

"Giovanni Bassi" <gb****@coair.com> wrote in message
news:eW**************@TK2MSFTNGP11.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


Nov 20 '05 #3

P: n/a
Giovanni,
In case you did not try it. My sample of just setting the DataSource to
distinct ListView objects:
Me.ListBox1.DataSource = New ListView(list)
Me.ComboBox1.DataSource = New ListView(list)
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

"Giovanni Bassi" <gb****@coair.com> wrote in message
news:uJ**************@tk2msftngp13.phx.gbl...
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
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message news:uH**************@TK2MSFTNGP10.phx.gbl...
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

"Giovanni Bassi" <gb****@coair.com> wrote in message
news:eW**************@TK2MSFTNGP11.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



Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.