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
rs.eof.movenext
loop

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.

Thanks

Recommended Answers

All 2 Replies

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
        Get
            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")))
rs.eof.movenext
loop

to get the tag then in code do:

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

works great.

Thanks a lot really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.