ArrayList, databinding and listbox

W

wooz

I've got ArrayList filled by some objects. I bind it to listbox.

Public class clsX
Private pUserName as string

Public Property UserName() As String
Get
Return pUserName
End Get
Set(ByVal Value As String)
pUserName = Value
End Set
End Property
End Class
-----
Dim Ar as new ArrayList
Dim x1 as new clsX
Dim x2 as new clsX

x1.UserName="aaa"
x2.UserNAme="bbb"
Ar.add(x1)
Ar.add(x2)

mylistbox.DataSource = Ar
mylistbox.DisplayMember = "UserName"
mylistbox.ValueMember = 'is assigned to other propoerty class X (not shown
above)

Now I get selected object in listbox:

dim tmpX as clsX
tmpX=mylistbox.SelectedItem()

Then i edit property "UserName"

tmpX.UserName="John"

Everything is fine but why this change is not seen in ArrayList Ar. Does
method "SelectedItem" of listbox returns a copy of object from ArrayList Ar,
or maybe listbox has copy of whole ArrayList Ar ?
 
A

Alex Feinman [MVP]

The only objects that can notify bound control of changes are those
implementing the IBindingList interface. ArrayList is not one of those. YOu
will either need to derive your own class from ArrayList and implement
IBindingList, use DataTable or rebind the ListBox to the ArrayList every
time the data changes.

Additionaly consider that when you change a property on an item that is an
element of an arraylist, arraylist is not aware of this change unless the
item class somehow signals this change up.
 

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