Listbox and Itemdata

Itemdata was given to a good tool to associate an index with items in Listbox and combobox. In VB.NET itemdata is not available. Is there any solution?

Comments

  • : Itemdata was given to a good tool to associate an index with items in Listbox and combobox. In VB.NET itemdata is not available. Is there any solution?
    :

    Well bad news is that the ItemData was lost in the transition to VB.NET. However, the new ListBox has a much more flexible scheme. You can basically create a custom class to represent the item data.

    Class ItemData
    Public FirstName As String
    Public LastName As String

    Sub New(ByVal firstName As String, ByVal lastName As String)
    Me.FirstName = firstName
    Me.LastName = lastName
    End Sub

    Overrides Function ToString() As String
    Return FirstName & " " & LastName
    End Function
    End Class

    ...

    You use this sample class like so, say in a form load event or something ...

    Private Sub Form1_Load( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load

    ListBox1.Items.Add(New ItemData("John", "Doe"))
    ListBox1.Items.Add(New ItemData("Jane", "Doe"))
    ListBox1.Items.Add(New ItemData("Tom", "Jones"))

    End Sub

    The ListBox control will display this in the list as ...

    John Doe
    Jane Doe
    Tom Jones

    Now the only reason why it list each item in "FirstName LastName" format is because the custom ItemData class overrides the ToString method. This means you can literally customize how the list items are rendered. Now accessing a ListItem is just as easy.

    Private Sub ListBox1_SelectedIndexChanged( _
    ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    ' Cast The Item To The Custom Item Data Class
    Dim item As ItemData = _
    DirectCast(ListBox1.Items(ListBox1.SelectedIndex), ItemData)

    MessageBox.Show(item.ToString)
    End Sub

    To demonstrate how to retieve an item I used the SelectedIndexChanged event, which fires when you select a ListItem. Notice that I casted the selected list item to my custom ItemData. Once casted, you can query any of its properties. In this case I use a message box to display "FirstName LastName."

    Clearly you should see how much more robust the new ListBox is. You can apply the same technique using structures as well, since structures can override a ToString method as well. Also, your custom ItemData class can host a multitude of properties, meaning you can associate many properties with a listitem.

    Since the listbox.Items.Add(...) takes an object, you can pass any data type really. The custom class technique seems to be the most robust.

    Hope this helps


  • : : Itemdata was given to a good tool to associate an index with items in Listbox and combobox. In VB.NET itemdata is not available. Is there any solution?
    : :
    :
    : Well bad news is that the ItemData was lost in the transition to VB.NET. However, the new ListBox has a much more flexible scheme. You can basically create a custom class to represent the item data.
    :
    : Class ItemData
    : Public FirstName As String
    : Public LastName As String
    :
    : Sub New(ByVal firstName As String, ByVal lastName As String)
    : Me.FirstName = firstName
    : Me.LastName = lastName
    : End Sub
    :
    : Overrides Function ToString() As String
    : Return FirstName & " " & LastName
    : End Function
    : End Class
    :
    : ...
    :
    : You use this sample class like so, say in a form load event or something ...
    :
    : Private Sub Form1_Load( _
    : ByVal sender As System.Object, _
    : ByVal e As System.EventArgs) Handles MyBase.Load
    :
    : ListBox1.Items.Add(New ItemData("John", "Doe"))
    : ListBox1.Items.Add(New ItemData("Jane", "Doe"))
    : ListBox1.Items.Add(New ItemData("Tom", "Jones"))
    :
    : End Sub
    :
    : The ListBox control will display this in the list as ...
    :
    : John Doe
    : Jane Doe
    : Tom Jones
    :
    : Now the only reason why it list each item in "FirstName LastName" format is because the custom ItemData class overrides the ToString method. This means you can literally customize how the list items are rendered. Now accessing a ListItem is just as easy.
    :
    : Private Sub ListBox1_SelectedIndexChanged( _
    : ByVal sender As Object, _
    : ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    :
    : ' Cast The Item To The Custom Item Data Class
    : Dim item As ItemData = _
    : DirectCast(ListBox1.Items(ListBox1.SelectedIndex), ItemData)
    :
    : MessageBox.Show(item.ToString)
    : End Sub
    :
    : To demonstrate how to retieve an item I used the SelectedIndexChanged event, which fires when you select a ListItem. Notice that I casted the selected list item to my custom ItemData. Once casted, you can query any of its properties. In this case I use a message box to display "FirstName LastName."
    :
    : Clearly you should see how much more robust the new ListBox is. You can apply the same technique using structures as well, since structures can override a ToString method as well. Also, your custom ItemData class can host a multitude of properties, meaning you can associate many properties with a listitem.
    :
    : Since the listbox.Items.Add(...) takes an object, you can pass any data type really. The custom class technique seems to be the most robust.
    :
    : Hope this helps
    :
    :
    :

    That's nice. I like it.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories