IList items "keeping position" | | |
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 | | | | re: IList items "keeping position"
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" <gbassi@coair.com> wrote in message
news:eWlfuIBjDHA.3060@TK2MSFTNGP11.phx.gbl...[color=blue]
> 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[/color]
happening?[color=blue]
>
> Thanks in advance for all the replies,
>
> Giovanni Bassi
>
>[/color] | | | | re: IList items "keeping position"
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]" <Jay_Harlow@email.msn.com> wrote in message
news:uHJxn4BjDHA.2416@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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[/color]
or[color=blue]
> 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[/color]
such[color=blue]
> 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" <gbassi@coair.com> wrote in message
> news:eWlfuIBjDHA.3060@TK2MSFTNGP11.phx.gbl...[color=green]
> > Hello All,
> >
> > I have a class implementing IList and a global object of this class[/color][/color]
type.[color=blue][color=green]
> > 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[/color][/color]
ListBox[color=blue][color=green]
> > SelectedItem changes.
> > I have no idea why this is happening. How can I prevent this from[/color]
> happening?[color=green]
> >
> > Thanks in advance for all the replies,
> >
> > Giovanni Bassi
> >
> >[/color]
>
>[/color] | | | | re: IList items "keeping position"
Giovanni,
In case you did not try it. My sample of just setting the DataSource to
distinct ListView objects:
[color=blue][color=green]
> > Me.ListBox1.DataSource = New ListView(list)
> > Me.ComboBox1.DataSource = New ListView(list)[/color][/color]
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" <gbassi@coair.com> wrote in message
news:uJarX5CjDHA.3568@tk2msftngp13.phx.gbl...[color=blue]
> 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[/color]
much,[color=blue]
> 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[/color]
object[color=blue]
> 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]" <Jay_Harlow@email.msn.com> wrote in[/color]
message[color=blue]
> news:uHJxn4BjDHA.2416@TK2MSFTNGP10.phx.gbl...[color=green]
> > Giovanni,
> > Oh! the joys of databinding. :-)
> >
> > When you bind the first ListBox to the list, an entry is made in the[/color][/color]
forms[color=blue][color=green]
> > BindingContext property, when you bind the ComboBox to the same list,[/color][/color]
the[color=blue][color=green]
> > 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[/color][/color]
IList[color=blue]
> or[color=green]
> > IBindingList that holds a reference to your original IList, delegating[/color][/color]
all[color=blue][color=green]
> > 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[/color]
> such[color=green]
> > 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)[/color][/color]
_[color=blue][color=green]
> > 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" <gbassi@coair.com> wrote in message
> > news:eWlfuIBjDHA.3060@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > Hello All,
> > >
> > > I have a class implementing IList and a global object of this class[/color][/color]
> type.[color=green][color=darkred]
> > > 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[/color][/color]
> ListBox[color=green][color=darkred]
> > > SelectedItem changes.
> > > I have no idea why this is happening. How can I prevent this from[/color]
> > happening?[color=darkred]
> > >
> > > Thanks in advance for all the replies,
> > >
> > > Giovanni Bassi
> > >
> > >[/color]
> >
> >[/color]
>
>[/color] |  | Similar Visual Basic .NET bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|