Hello Everyone, I hope everyone is having a nice day ;-)

I have a question about combo box and here is it...

I Have sex combo box(Male or Female only) and i want that when i choose Female it will have an id of 2(Male = 1, Female = 2)
without using SQL just VB.net only....

How should i do that? in such a way that, when i choose in a combo box about sex, it will have a corresponding number like 1 for Male or 2 for Female

Recommended Answers

All 3 Replies

Hi,
The combobox will take objects rather than just strings as it's items.

I tend to make a custom class I call it dataitem and give it a string property and a data property. You then can override the ToString to return your String value:

Class DataItem

Private _Data as object
Private _Text as string

Public Property Data as object
Get()
    Return _Data
End Get
Set (byVal value as object)
    _Data = value
End Set
End Property

Public Property Text
Get()
    Return _Text
End Get
Set (byVal value as string)
    _Text = value
End Set
End Property

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

Sub New (Optional byVal Data as Object = nothing, Optional ByVal Text as String ="")
    _Text = Text
    _Data = Data
End Sub

End Class

Now you can add your combobox items like this:

MyComboxbox.items.add( new DataItem(1,"Male"))
MyComboxbox.items.add( new DataItem(2, "Female"))

Then when you want to read the value:

dim DI as DataItem
dim MySex as integer
DI = MyCombobox.SelectedItem

MySex = cint(DI.Data)

Thanks my friend..i will try your logic :-)

Just remember to use the override ToString function so that it displays correctly in the Combobox!

commented: Mmm Love method overloading! +9
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.