tags:

views:

44

answers:

2

Okay, so the title was horrible. I know that. Here's what I'm trying to do:

I have a lot of data that's coming from a database and being adding to a listbox. The data that's coming from the database is a unique ID, and a name.

Is there any way I can make the item contain both the ID and name? I know I can append it, that's not what I'm looking to do. I need a way to be able to either get the ID, or get the name, while displaying them both.

I have gotten as far as creating a class:

Class Item
    Property ID as Integer
    Property Name as String

    Sub New(ID as Integer, Name as String)
        Me.ID = ID
        Me.Name = Name
    End Sub

    Overrides Function ToString() as String
        Return Name
    End Function
End Class

That looks like it should do the trick, but I'm having trouble getting the ID, instead of the name. To put it simply, I wish I could do this: listbox1.selecteditem(id) to get the id, or listbox1.selecteditem(name) to get the name.

Please help!

A: 

I think you'll have to write a helper method to do this. If you're using VB 3.5 or newer (part of VS2008 and newer) you can write an extension method so that you can at least get nice syntax. You could write one such that it looked like:

listbox1.SelectByID(123)
listbox1.SelectByName("hello")

In the methods you'd have some search algorithm that went through the items and found the right one.

Eilon
+2  A: 

You can bind to a List(Of Item) like this:

Dim ItemList = New List(Of Item)
' Fill the list with appropiate values.'
listbox1.DataSource = ItemList
listbox1.DisplayMember = "Name"
listbox1.ValueMember = "ID"

Then listbox1.SelectedValue holds the ID and you can access the name like this:

DirectCast(listbox1.SelectedItem, Item).Name

If you want to show both the ID and the Name, then I suggest you add a property to be the displayed value in the Itemclass:

Public ReadOnly Property DisplayedValue() as String
    Get
        Return Me.Name & " (" & Me.ID.ToString & ")"
    End Get
End Property

Then when binding the list make

 listbox1.DisplayMember = "DisplayedValue"

Update:

Based on your comments below I'd say my solution still works. However with this methodology the items must be added to the list and then the list bound to the object. The items can not be added individually and directly to the list box (as you would be separating data from presentation I don't see that as a problem).

To show a message box with the selected item then you just need to do:

MessageBox.Show(DirectCast(listbox1.SelectedItem, Item).ID.ToString))
Wilhelm
You're amazing. Thank you!
Josh streit
Wait wait wait! Not so much praise yet. How would I do this for each separate item!?/facepalm.
Josh streit
When binding to a list every item in the list is treated the same. In a way you are binding to the list, not every item. ¿Or am I misundertanding your question?
Wilhelm
¡You are misunderstanding my question! (see what I did there?)Each item that's being added to the listbox contains a name, and corresponding unique ID (that's being pulled from the rows of a database).For example, I have the following data:UniqueID/Name:123/BOB324/Wilhelm595/DumbieI want the listbox item string to be, for example: Wilhelm (324). When the user selects that item, and press a button, I'd like to (again, for example) display the ID - 324 - in a messagebox.Thanks in advance!
Josh streit