Click here to Skip to main content
Click here to Skip to main content

Quick And Dirty Option Lists using Enums

By , 22 Feb 2011
 
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
    'Populate a combo box with the enum options
    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
    'Retrieve the Enum Text Name from the Index selected
    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. :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

DaveAuld
Engineer Apache North Sea Ltd.
United Kingdom United Kingdom
Member
I have been working in the Oil & Gas Industry for over 20 years now.
 
Core Discipline is Instrumentation and Control Systems.
 
Completed Bsc Honours Degree (B29 in Computing) with the Open University in 2012.
 
Currently, Offshore Installation Manager for the Forties Charlie Platform, which is located ~110Miles NE of Aberdeen, Scotland in the North Sea.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 Useful tip! Simple yet very practica...memberDrABELL22 Feb '11 - 16:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 23 Feb 2011
Article Copyright 2011 by DaveAuld
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid