473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Nov 20 '05 #1
3 4503
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark Bordner | last post by:
Hi All, Given the following XML, I've been trying to figure out what XPATH expression will give me the "position" of a <lineitem> within the <shipto> that is its grandparent. To give you an...
1
by: nobody | last post by:
hi there! given <!ELEMENT a (#PCDATA | x)*> <!ELEMENT x (#PCDATA)> how can I find out if x is "embedded" at the beginning <a><x>xxx</x>aaa</a> or at the end <a>aaa<x>xxx</x></a> or in the...
0
by: Luigi | last post by:
I would know what "criteria" is used in box positioning with the position property. Here my tries (using IE and Opera): I have a box with an absolute positioning (properties used: position,...
2
by: Jessard | last post by:
Hi All, I am writing a .NET app that accesses an access View. The view has a table column called 'Position'. When i try and right an UpdateCommand string for this using this column I am...
4
by: Ghyslaine Crespe | last post by:
Hello, In my script, the line document.getElementById(id).style.background-position = "-200px -500px"; fails ! So, how can I change the background-position value ?
1
by: tschoepi | last post by:
Hi there, if I try to access a table named Position in an Access-Database via OleDbDataAdapter I get an error. If I rename the table to e.g. Positions it works. Any idea how to solve this...
2
by: petermichaux | last post by:
Hi, I tried the following and everything worked fine. element.style.position="relative"; Then I tried to make the CSS rule important and it didn't work. The positioning was all wrong in...
3
by: rsrimantula | last post by:
I have a table with html code <style> div.scroll { height:150px; overflow:auto; } table.scrollable th { position:relative;
1
by: vunet.us | last post by:
Mozilla reported the fix to this bug: https://bugzilla.mozilla.org/show_bug.cgi?id=167801. When input text field is located over div, the cursor cannot be seen unless special CSS properties are...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.