Results 1 to 8 of 8

Thread: [RESOLVED] ListBox Display Member

  1. #1

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Resolved [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().
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    Last edited by Vectris; Dec 21st, 2009 at 07:30 PM.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] ListBox Display Member

    Quote Originally Posted by Vectris View Post
    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:
    1. Structure Song
    2.  
    3.     Private _name As String
    4.     Private _path As String
    5.  
    6.     Public Property Name() As String
    7.         Get
    8.             Return _name
    9.         End Get
    10.         Set(ByVal value As String)
    11.             _name = value
    12.         End Set
    13.     End Property
    14.  
    15.     Public Property Path() As String
    16.         Get
    17.             Return _path
    18.         End Get
    19.         Set(ByVal value As String)
    20.             _path = value
    21.         End Set
    22.     End Property
    23.  
    24. 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:
    1. Structure Song
    2.  
    3.     Public Property Name As String
    4.     Public Property Path As String
    5.  
    6. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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?
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width