Populate listview from custom class - selectedvalue always null...

PatM

Well-known member
Joined
Dec 5, 2012
Messages
52
Programming Experience
10+
I know it'll be something dumb but I just can't figure out how to properly set the displaymember and valuemember. THis is what I have so far.

VB.NET:
    Private Class cControllerListBoxEntry
        Private m_strName As String
        Private m_strSerial As String

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

        Public Property Serial() As String
            Get
                Return m_strSerial
            End Get
            Set(ByVal Value As String)
                m_strSerial = Value
            End Set
        End Property

        Public Overrides Function ToString() As String
            Return m_strName
        End Function
    End Class

Form load:

VB.NET:
        Dim myEntry As cControllerListBoxEntry = New cControllerListBoxEntry

        lsbControllers.DisplayMember = "Name"
        lsbControllers.ValueMember = "Serial"

        myEntry.Name = "Machine2-192.168.1.100"
        myEntry.Serial = "799342"

        lsbControllers.Items.Add(myEntry)

Then when I double click the selectedvalue is null.
VB.NET:
   Private Sub lsbControllers_DoubleClick(sender As Object, e As System.EventArgs) Handles lsbControllers.DoubleClick

        MsgBox(lsbControllers.SelectedItem.ToString & " " & Str(lsbControllers.SelectedValue.ToString))

    End Sub

Could someone point out my mistake please? Thanks!
 
Actually, I just figure out a way in the doubleclick. It's not exactly what I was trying to do but what I was trying to do was probably wrong anyway 8)

dim controller as cControllerListBoxEntry
controller=lsbControllers.SelectedItem
msgbox(controller.Name & " " & controller.Serial)
 
VB.NET:
        Private m_strName As String
        Private m_strSerial As String

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

        Public Property Serial() As String
            Get
                Return m_strSerial
            End Get
            Set(ByVal Value As String)
                m_strSerial = Value
            End Set
        End Property
Have a look at this: Auto-Implemented Properties (Visual Basic)
 
Thats awesome. My class is a tiny little thing now 8) And the override return is simply Name.
 
Back
Top