Populate Combobox with Enumerators

bpayne111

Junior Contributor
Joined
Feb 28, 2003
Messages
325
Location
BFE
I'm trying to find the best way to fill a combobox with an enumerator i created
I'm sure this is a one-liner but, i can't seem to find the magic method i'm looking for.

Any ideas?
 
I'm not quite sure what you mean.. do you mean fill the ComboBox with all the names in an Enum? Like if you have..

enum MyEnum { Stuff1, Stuff2, Stuff3 }

..you'd want to put the words (ie; "Stuff1") and not the value (ie; 0) contained inside the enum? Assuming their values are in order and one after another (ie; Stuff1 = 0, Stuff2 = 1, Stuff3 = 2, etc..) you can do it using a for loop.
 
As far as I know, you can't get the names of the Enum values, as
they don't exist (AFAIK) anywhere but inside the IDE. Once the
program is compiled, I think the Enum values are simply replaced with
their integer values.
 
Use something like this:

Visual Basic:
Public Enum MyEnum
    FirstThing
    SecondThing
    AnotherThing
End Enum

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim s As String

    For Each s In [Enum].GetNames(GetType(MyEnum))
        ComboBox1.Items.Add(s)
    Next
End Sub

You can then use [Enum].Parse to get the numeric data equivalent for any string once it has been picked.
 
Heh, cool. I didn't know that. Perhaps I am thinking of VB6.

Learn something new every day. :)
 
thanks divil i'll use that
this is what i came up with... when run, i get the error "Null Reference Execption" when my code is executed. can anyone figure out what is null???
ComboBox1.Items.AddRange(styles.GetValues(Type.GetType("Styles")))
Visual Basic:
Public Enum styles
    br = 7
    jpd = 9
End Enum

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.Items.AddRange(styles.GetValues(Type.GetType("Styles")))
    End Sub
End Class
 
Visual Basic:
ComboBox1.Items.AddRange(styles.GetValues(Type.GetType("Styles")))
It looks like that line should read:
Visual Basic:
ComboBox1.Items.AddRange([Enum].GetNames(GetType(styles)))
 
yes indeed that works wonderfully...

ok so this brings a new question
what is the difference in
Type.GetType("Styles") and
GetType(Styles)
 
Hmm.. cool, I didn't know you could loop through 'em using a for each, I was thinking of a regular 'ol for loop using the individual names in replace of integers (as you would in C++).

Does anyone by chance know the GetType() equiv for C#?

Volte:
You can get the name of an enum value by simply attaching ToString to the end of the variable (or enum value). In C# it uses the actual name as default.
 
Last edited:
Ahh.. there it is. *mumbles* Why must they keep changing basic things between languages to confuse us? :confused:
 
You can also use a Text description to get back to the enum value, in case you want to store the Enum value by name somewhere (we have a need - strange rare case, but necessary)

If anyone cares, I'll try and dig up the code. :)

-Nerseus
 
Visual Basic:
MessageBox.Show([Enum].Parse(GetType(Animals), "Dog"))
If the Enum was defined
Visual Basic:
Private Enum Animals
  Bird = 1
  Cow = 2
  Dog = 3
End Enum
It would return 3.
 
couldn't you just use
Visual Basic:
MessageBox.Show(Animal.Dog)

i thought that would return 3
if it does why would you need the others?
 
Visual Basic:
MessageBox.Show([Enum].Parse(GetType(Animals), "Dog"))
so where does this line become of use?
 
Back
Top