Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 lazy i'm just resting before i get tired.
Posted

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.

Gamer extraordinaire. Programmer wannabe.
  • *Experts*
Posted

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.

  • *Gurus*
Posted

Use something like this:

 

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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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")))

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

i'm not lazy i'm just resting before i get tired.
  • *Experts*
Posted
ComboBox1.Items.AddRange(styles.GetValues(Type.GetType("Styles")))

It looks like that line should read:

ComboBox1.Items.AddRange([Enum].GetNames(GetType(styles)))

Posted

yes indeed that works wonderfully...

 

ok so this brings a new question

what is the difference in

Type.GetType("Styles") and

GetType(Styles)

i'm not lazy i'm just resting before i get tired.
Posted (edited)

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.

Edited by wyrd
Gamer extraordinaire. Programmer wannabe.
Posted
Ahh.. there it is. *mumbles* Why must they keep changing basic things between languages to confuse us? :confused:
Gamer extraordinaire. Programmer wannabe.
  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted
MessageBox.Show([Enum].Parse(GetType(Animals), "Dog"))

If the Enum was defined

Private Enum Animals
 Bird = 1
 Cow = 2
 Dog = 3
End Enum

It would return 3.

Posted

couldn't you just use

MessageBox.Show(Animal.Dog)

 

i thought that would return 3

if it does why would you need the others?

i'm not lazy i'm just resting before i get tired.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...