Results 1 to 6 of 6

Thread: Convert Integer to Value from Enum

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Convert Integer to Value from Enum

    I have add some items into an combbox, the items represent the values from ContentAlignment Enum, (the values of the combobox are not same as the values of the Enum)

    Upon selection of a value from the combobox, i want to set the TextAlignment of the Button

    Plz Help

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Convert Integer to Value from Enum

    I don't quite understand what you want Value to be. Regardless, you can convert an enum to its int value like so:

    Code:
    CInt(MyEnum.MyValue)
    or create an enum from a number or string

    Code:
    DirectCast(System.Enum.Parse(GetType(MyEnum), "MyValue"), MyEnum)

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Convert Integer to Value from Enum

    Code:
            'use the enum as values for the combobox
            ComboBox1.DataSource = [Enum].GetValues(GetType(ContentAlignment))
            'set index for test
            ComboBox1.SelectedIndex = 1
            'change the alignment
            Button1.TextAlign = CType(ComboBox1.SelectedIndex, ContentAlignment)
    If you don't want all the values from the enum then

    Code:
            'use the enum as values for the combobox, excluding some values
            Dim ds = [Enum].GetValues(GetType(ContentAlignment)). _
                Cast(Of ContentAlignment)().Where(Function(ca) ca <> ContentAlignment.BottomLeft AndAlso ca <> ContentAlignment.BottomRight)
            ComboBox1.DataSource = ds.ToList
    Last edited by dbasnett; Feb 4th, 2011 at 12:08 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Convert Integer to Value from Enum

    dbasnett's code is the way to go except that you should be casting the SelectedItem rather than the SelectedIndex. In some cases the result will be the same but not in all, so stick with the one that will always work, i.e. SelectedItem.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    New Member
    Join Date
    Dec 2019
    Posts
    1

    Re: Convert Integer to Value from Enum

    Hey all I know this is an old thread but it comes up fairly high in results. I wanted to drop this code example for anyone finding this .

    Code:
    //Valid 
    Color c = (Color)3; 
    //Result: c = Blue
    
    //Invalid
    Color c = (Color)4;
    //Result: c = 4
    Snippet borred from:C# int to enum

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Convert Integer to Value from Enum

    Yeah, that's a snippet, but this is a VB forum, so a C# snippet isn't quite so appropriate. How about turning it into VB?
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width