Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

can i assign values to combo box drop down list?

hello all!
i would like to ask u if i could assign values to my combo box' items.
i have a combo box with 2 different options. In my programm i have to pass as a parameter an integer, depending on the choice of the user. so if the user chooses "distance" the integer i pass is 1 and if he chooses "time" the integer value i pass as argument is 2. I would like to know if i can somehow bind each choice with a number (distance-1,time-2) so as to avoid the checks with if clause. this is how my programm looks like now:

Dim mode As Integer
If (cmbMode.SelectedIndex.Equals(-1)) Then
MessageBox.Show("You must select type first!")
Exit Sub
Else
If (Me.cmbMode.Text.Equals("Distance")) Then
mode = 1
ElseIf (Me.cmbMode.Text.Equals("Time")) Then
mode = 2
End If
End If

Any help appreciated!!:)
[976 byte] By [natasa] at [2007-11-11 11:57:36]
# 1 Re: can i assign values to combo box drop down list?
You can create an Enum to contain the values and their descriptions:

Public Enum Modes
Distance = 1
Time = 2
End Enum

Bind the Enum to the ComboBox:

Private Sub MyForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
cmbMode.DataSource = System.Enum.GetValues(GetType(Modes))
End Sub

Then simply read the ComboBox's SelectedValue into an Integer variable:

Dim Mode As Integer = CInt(cmbMode.SelectedValue)
Phil Weber at 2007-11-11 20:42:36 >
# 2 Re: can i assign values to combo box drop down list?
thank you very much phil for your reply!

that was indeed what i needed!!
natasa at 2007-11-11 20:43:33 >
# 3 Re: can i assign values to combo box drop down list?
But, doesn't this still require an If statement to find out what the current value of Mode is?
Hack at 2007-11-11 20:44:31 >
# 4 Re: can i assign values to combo box drop down list?
In the original question, he said that he's passing the value of Mode as a parameter to another function. There's probably an If statement in that function, but that's not our problem. ;-)
Phil Weber at 2007-11-11 20:45:37 >