Computing.Net > Forums > Programming > VB.NET ListBox DisplayMember

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

VB.NET ListBox DisplayMember

Reply to Message Icon

Original Message
Name: Urkle10
Date: February 6, 2006 at 12:27:36 Pacific
Subject: VB.NET ListBox DisplayMember
OS: Windows XP
CPU/Ram: P4 3.0 GHz
Comment:

IDE: Microsoft Visual Studio .NET 2003
Language: Visual Basic .NET 2003

I am using a resource DLL to access an external database via OLE/COM. There is very little documentation aside from what methods are available and I do not have access to the source code. For all intents and purposes, each database item is returned as a DBobject collection.

I have a project wherein I created a ListBox control called Box. In my application, I add items to the list as follows.

Dim List As DBobject = anExistingCollection
Dim Item As DBobject
For Each Item In List
    Box.Items.Add(Item)
Next

From the Object Explorer, I found that there is a property defined in the DLL as follows.

Public Overridable Default Property [Property](Optional ByVal Type As Object = Nothing, Optional ByVal Index As Object = Nothing) As Object

In the code snippet below, I get the selected item and set a label control text value to the name of the DBobject since the "Property" property returns the name of the object by default.

Dim tempDBobject As DBobject
tempDBobject = Box.SelectedItem
Label1.Text = tempDBobject.Property

I set the DisplayMember of the ListBox to "Property" in the design view. When the ListBox is populated, every item is listed as being "System.__ComObject" but the Box.SelectedItems method returns the proper name value to be displayed in Label1.

I even tried the following as the DisplayMember in the design view with identical results. (Note that PropertyGet("Name") is another method to obtain the name value.)

Property
[Property]
"Property"
PropertyGet("Name")

I wrote a sample program with a class of my own design with a property that returned a String as an Object for the DisplayMemeber. It worked flawlessly. Does anyone have any suggestions as to why the proper text isn't being displayed in the ListBox for my real application? Thanks!

Urkle10


Report Offensive Message For Removal


Response Number 1
Name: Urkle10
Date: February 9, 2006 at 07:44:21 Pacific
Reply: (edit)

It turns outs that the DisplayMember and ValueMember properties are only used if the ListBox DataSource is set to an object that implements the IList interface. Since I added items individually using the Add method instead of setting the DataSource directly, the ListBox ignored DisplayMember and ValueMember.

Urkle10


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software