I am taking user names and id from a database and dropping it into a combo box for users to select there name. in vb 6 i used to do the following:

do until rs.eof = true
with me.combobox.comboitems.add(,, rs.fields("Name"))
.tag = rs.fields("ID")
end with

the combobox text would hold the name, and the tag would hold the id hidden from the user..

in vb.net 2008, i am having a hard time setting the .tag for combo items and have basically given up on it and turned to new measures.

one, i was going to load the users table into a localdatabasecache, and use the IDs and names from there, which i can't seem to get to work properly (mysql database, it only wants to accept microsoft sql)

second, create an array that holds the ids and just select the same index of the array vs the index of the combobox, which i cant get to work right (not good with arrays)

third is to just pull the id again after the name is connected - which causes another database connection and isn't really want i want to do.

question: what is the best method to go about this in .net 08

any help with this would be greatly appreciated.


7 Years
Discussion Span
Last Post by jlego

i would go with the Tag property.
create a class in your project:

Public Class ComboItem
    Private m_Tag As String
    Private m_text As String

    Public Sub New(ByVal Text As String, ByVal Tag As String)
        m_text = Text 'normal text
        m_Tag = Tag ' additional infos
    End Sub

    Public ReadOnly Property Tag As String
            Return m_Tag
        End Get
    End Property

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

End Class

then you simply add the db stuff like

do until rs.eof 
Combobox1.Items.Add(New ComboItem(rs.fields("Name"), rs.fields("ID")))

to get the tag then in code do:

msgbox(CType(Combobox1.SelectedItem, ComboItem).Tag)

Edited by GeekByChoiCe: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.