Click here to Skip to main content
13,598,689 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


11 bookmarked
Posted 22 Feb 2011
Licenced CPOL

Quick And Dirty Option Lists using Enums

, 3 Jun 2014
Rate this:
Please Sign up or sign in to vote.
Using Enum to provide a quick and easy option lists

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
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
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. Smile | :)

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;


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


About the Author

Scotland Scotland
I have been working in the Oil & Gas Industry for over 25 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 in the Al Shaheen oil field, which is located off the coast of Qatar.

Prior to this, 25 years of North Sea Oil & Gas experience.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Sergey Alexandrovich Kryukov3-Jun-14 5:30
mvpSergey Alexandrovich Kryukov3-Jun-14 5:30 
QuestionBe careful, everyone! It's not so simple in some cases Pin
Sergey Alexandrovich Kryukov3-Jun-14 5:09
mvpSergey Alexandrovich Kryukov3-Jun-14 5:09 
AnswerRe: Be careful, everyone! It works properly not in all cases. Pin
DaveAuld3-Jun-14 5:18
protectorDaveAuld3-Jun-14 5:18 
GeneralRe: Be careful, everyone! It works properly not in all cases. Pin
Sergey Alexandrovich Kryukov3-Jun-14 5:29
mvpSergey Alexandrovich Kryukov3-Jun-14 5:29 
GeneralReason for my vote of 5 Useful tip! Simple yet very practica... Pin
DrABELL22-Feb-11 16:37
memberDrABELL22-Feb-11 16:37 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.180621.3 | Last Updated 3 Jun 2014
Article Copyright 2011 by DaveAuld
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid