bpayne111 Posted March 31, 2003 Posted March 31, 2003 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? Quote i'm not lazy i'm just resting before i get tired.
wyrd Posted March 31, 2003 Posted March 31, 2003 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. Quote Gamer extraordinaire. Programmer wannabe.
*Experts* Volte Posted March 31, 2003 *Experts* Posted March 31, 2003 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. Quote
*Gurus* divil Posted March 31, 2003 *Gurus* Posted March 31, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Volte Posted March 31, 2003 *Experts* Posted March 31, 2003 Heh, cool. I didn't know that. Perhaps I am thinking of VB6. Learn something new every day. :) Quote
bpayne111 Posted March 31, 2003 Author Posted March 31, 2003 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 Quote i'm not lazy i'm just resting before i get tired.
*Experts* Volte Posted March 31, 2003 *Experts* Posted March 31, 2003 ComboBox1.Items.AddRange(styles.GetValues(Type.GetType("Styles")))It looks like that line should read:ComboBox1.Items.AddRange([Enum].GetNames(GetType(styles))) Quote
Moderators Robby Posted March 31, 2003 Moderators Posted March 31, 2003 divil, nice one. I learn something new every day. :) Quote Visit...Bassic Software
bpayne111 Posted April 1, 2003 Author Posted April 1, 2003 yes indeed that works wonderfully... ok so this brings a new question what is the difference in Type.GetType("Styles") and GetType(Styles) Quote i'm not lazy i'm just resting before i get tired.
wyrd Posted April 1, 2003 Posted April 1, 2003 (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 April 1, 2003 by wyrd Quote Gamer extraordinaire. Programmer wannabe.
*Gurus* divil Posted April 1, 2003 *Gurus* Posted April 1, 2003 GetType(ClassName) typeof(ClassName) Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
wyrd Posted April 1, 2003 Posted April 1, 2003 Ahh.. there it is. *mumbles* Why must they keep changing basic things between languages to confuse us? :confused: Quote Gamer extraordinaire. Programmer wannabe.
*Experts* Nerseus Posted April 1, 2003 *Experts* Posted April 1, 2003 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 Quote "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* Volte Posted April 1, 2003 *Experts* Posted April 1, 2003 MessageBox.Show([Enum].Parse(GetType(Animals), "Dog"))If the Enum was definedPrivate Enum Animals Bird = 1 Cow = 2 Dog = 3 End EnumIt would return 3. Quote
bpayne111 Posted April 2, 2003 Author Posted April 2, 2003 couldn't you just use MessageBox.Show(Animal.Dog) i thought that would return 3 if it does why would you need the others? Quote i'm not lazy i'm just resting before i get tired.
wyrd Posted April 2, 2003 Posted April 2, 2003 .. and Animal.Dog.ToString() would return the name variation ("Dog"). :) Quote Gamer extraordinaire. Programmer wannabe.
bpayne111 Posted April 2, 2003 Author Posted April 2, 2003 MessageBox.Show([Enum].Parse(GetType(Animals), "Dog")) so where does this line become of use? Quote i'm not lazy i'm just resting before i get tired.
Moderators Robby Posted April 3, 2003 Moderators Posted April 3, 2003 FYI, the Option Strict and VB Functions Discussion are continued here http://www.xtremedotnettalk.com/showthread.php?s=&threadid=71107 Quote Visit...Bassic Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.