listbox dilemma

Anti-Rich

Well-known member
Joined
Jul 1, 2006
Messages
325
Location
Perth, Australia
Programming Experience
1-3
hi all,

i have a question that has stumped me for quite some time

the listbox has a method SelectedValue which gets the value of the ValueMember property for that item (i think).

my question is, how do i actually set a value per item...

the reason i ask, is i want to add a name to a listbox, and then when i click the name i would set the id number of the person would be the 'value' of the item.

can someone please help me?

cheers

adam
 
the reason i ask, is i want to add a name to a listbox, and then when i click the name i would set the id number of the person would be the 'value' of the item.
I have no idea what you mean by that. Are you wanting to set the value after selecting a listbox item or simply retrieve it? (The code below accounts for both).

If you're not databinding the listbox, the SelectedValue really has no meaning. Here's a way to make it work without databinding:
The ListBox.Item.Add method accepts an object so you'll want to create a Class with Name & ID properties. Create an instance of the Class and add it to the ListBox. When an item is selected, get the selected item and cast it to the Class you created and retrieve it's Id property. In the example code below, the secret to displaying something readable in the ListBox is to have a ToString method of the Class. To use the code, create a form with a ListBox (ListBox1), a NumericUpDown (nudID), and a Button (cmdSetId).
VB.NET:
Expand Collapse Copy
Public Class TestUnBoundListBox

    Private Sub TestListBox_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Load
        Dim prsn As New Person("Paszt", 1)
        ListBox1.Items.Add(prsn)
        prsn = New Person("Anti-Rich", 2)
        ListBox1.Items.Add(prsn)
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        nudId.Value = CType(ListBox1.Items(ListBox1.SelectedIndex), Person).Id
    End Sub

    Private Sub cmdSetId_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles cmdSetId.Click
        CType(ListBox1.Items(ListBox1.SelectedIndex), Person).Id = CInt(nudId.Value)
    End Sub

End Class

Friend Class Person

    Public Sub New(ByVal Name As String, ByVal Id As Integer)
        _name = Name
        _id = Id
    End Sub

    Private _name As String = String.Empty
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Private _id As Integer
    Public Property Id() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return _name
    End Function

End Class
If you are databinding then you simply set the DataSource, DisplayMember, and ValueMember properties accordingly.
 
Back
Top