[RESOLVED] ListBox Display Member
I've set my DisplayMember property of the listbox to the property I want yet when I add the objects to my listbox it "fails" and instead uses the .ToString of the entire object which doesn't return what I want.
I searched around and according to this topic:
http://www.computing.net/answers/pro...ber/13934.html
the DisplayMember only works if your listbox has a datasource. Well I'm not using a DataSource, like that guy I'm just using .Add. So how do I get DisplayMember to work without using a DataSource?
Also I tried putting all of the objects (their simple item structures) into a List Of and then set that list as the DataSource to the ListBox and it actually added all of the objects to the listbox however the DisplayMember still didn't work and all I got where the results of Object.ToString().
Re: ListBox Display Member
The DisplayMember works whether the control is bound or not (although that may not have been the case in .NET 1.x) . It's the ValueMember that requires data-binding. If it's not working for you then either something is corrupted or else you're doing something wrong. Try the following and let me know if it works:
1. Create a new WinForms project.
2. Add 2 ListBoxes.
3. Add the following code:
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.ListBox1.DisplayMember = "FirstName"
Me.ListBox1.Items.Add(New Person("Peter", "Smith"))
Me.ListBox1.Items.Add(New Person("Paul", "Jones"))
Me.ListBox1.Items.Add(New Person("Mary", "Williams"))
Dim people As New List(Of Person)
people.Add(New Person("John", "Johnson"))
people.Add(New Person("Jane", "Edwards"))
people.Add(New Person("Alan", "Green"))
Me.ListBox2.DisplayMember = "LastName"
Me.ListBox2.DataSource = people
End Sub
End Class
Public Class Person
Private _firstName As String
Private _lastName As String
Public Property FirstName() As String
Get
Return _firstName
End Get
Set(ByVal value As String)
_firstName = value
End Set
End Property
Public Property LastName() As String
Get
Return _lastName
End Get
Set(ByVal value As String)
_lastName = value
End Set
End Property
Public Sub New(ByVal firstName As String, ByVal lastName As String)
Me._firstName = firstName
Me._lastName = lastName
End Sub
End Class
4. Run the project.
What do you see? Does the first ListBox show Peter, Paul and Mary while the second shows Johnson, Edwards and Williams? If so then your current issue is almost certainly because you're doing something wrong.
Re: ListBox Display Member
Your example worked fine. My problem was I wasn't using a class, I was using a Structure. As soon as I switched over to a class the DisplayMember worked perfectly. Thanks jmc.
Re: [RESOLVED] ListBox Display Member
As far as I'm aware, that shouldn't matter. I can't test that right now but I will when I get to work. Are you sure you weren't just trying to bind a field instead of a property?
Re: [RESOLVED] ListBox Display Member
I'm pretty sure it was a property, I don't actually know what you mean by field, but here is what my structure was.
Code:
Structure Song
Dim sName as String
Dim sPath as String
End Structure
I used "sName" as the DisplayMember and whenever I added a variable of the type Song it would simply display Song.ToString.
EDIT: Here I tested it as simply as possible, this doesn't work either but it's what I was trying to do.
Code:
Option Strict On
Public Class Form1
Dim Names() As String = {"Song1", "Song2", "Song3"}
Dim Paths() As String = {"Path1", "Path2", "Path3"}
Structure Song
Dim sName As String
Dim sPath As String
Public Sub New(ByVal tName As String, ByVal tPath As String)
sName = tName
sName = tPath
End Sub
End Structure
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.DisplayMember = "sName"
For x As Integer = 0 To 2
ListBox1.Items.Add(New Song(Names(x), Paths(x)))
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(CType(ListBox1.SelectedItem, Song).sName)
End Sub
End Class
Button 1 was simply to test that the Song objects being added to the listbox were actually holding the correct values, which they were, but their still not being displayed properly.
Re: [RESOLVED] ListBox Display Member
Quote:
Originally Posted by
Vectris
I'm pretty sure it was a property, I don't actually know what you mean by field, but here is what my structure was.
Code:
Structure Song
Dim sName as String
Dim sPath as String
End Structure
A field is a member variable, and those are fields. Does it say "Property" in the declaration of sName or sPath? If not then it's not a property. In fact, both declarations contain "Dim", which is specifically for declaring variables. If they were properties they would look like this:
vb.net Code:
Structure Song
Private _name As String
Private _path As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Path() As String
Get
Return _path
End Get
Set(ByVal value As String)
_path = value
End Set
End Property
End Structure
VB 2010 will introduce auto-properties, which will make it much simpler to declare properties that need no extra logic:
vb.net Code:
Structure Song
Public Property Name As String
Public Property Path As String
End Structure
One of the reasons that you should always use properties in preference to public fields is exactly this: you can only bind properties, not fields.
Re: [RESOLVED] ListBox Display Member
Ok thanks, I had no idea what the difference was between a field or property but I get it now. I didn't even think to use the a Property in the Structure as I thought that was something only classes had, I see I was definitely wrong though.
If both structures and classes can hold properties and procedures then what's the difference between those two?
Re: [RESOLVED] ListBox Display Member
The main difference is that structures are value types and classes are reference types. As usual, the first place you should seek information is MSDN. I know for a fact that it has a topic dedicated specifically to the differences between classes and structures.