You might have a form that sets some options, and you need a quick and easy way to get the value from the option selected or determine what was selected.
Well this is achievable using Enum
(erators):
Take this example;
Private Enum ListOfChoices
Yes
No
Possibly
Never
Pass
End Enum
It is possible to quickly and easily dump this list into a dropdown list combo box, and then quickly pull the value the user selected.
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ComboBox1.Items.AddRange([Enum].GetNames(GetType(ListOfChoices)))
End Sub
Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
MsgBox("You Selected: " & [Enum].GetName(GetType(ListOfChoices), ComboBox1.SelectedIndex))
End Sub
You could have just read the combobox
selected value, but the point was demonstrating get a Enum String
from the Enum
value.
Both the Enum
and the Combobox
have 0 based index values for the item lists, so it is easy to work them together. This is just as applicable to list boxes, etc.
Cheap and easy.
WARNING! As it says it is Quick and Dirty, it could just as easily cause you a lot of problems - Refer to this article for indepth discussion around enumerating enum's; http://www.codeproject.com/Articles/129830/Enumeration-Types-do-not-Enumerate-Working-around