ListBox Control Display Object Type

B

Brian

I have a Collection of custom objects (MyObject) that I am trying to
databind to a ListBox.

It works every other time I call ListBox1.DataSource = MyObjectCollection
I have set the DisplayMember to Name, which is a property of MyObject, the
one that I want to show. Sometimes it works and sometime I get a bunch of
Assembly.NameSpace.MyObject items in there?

Any idea what I am doing wrong, or how I can get around this?

Thanks
 
T

Tim Wilson

The code below allows custom objects, stored in an ArrayList, to be bound to
a ListBox...

Public Class MyObject
Private nameValue As String

Public Property Name() As String
Get
Return nameValue
End Get
Set(ByVal Value As String)
nameValue = Value
End Set
End Property

Public Sub New(ByVal name As String)
Me.Name = name
End Sub
End Class

....

Dim a As New ArrayList
a.Add(New MyObject("John"))
a.Add(New MyObject("Jane"))

Me.ListBox1.DataSource = a
Me.ListBox1.DisplayMember = "Name"
 

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