Filling a listbox with the values in a enumeration

Discussion in 'ASP .Net' started by Timothy Parez, Mar 3, 2004.

  1. Hello,

    I have an enumeration for example

    enum Speed
    {
    slow,
    fast,
    faster
    }

    Is there a way to bind this to a listbox ?
    so that the listbox displays the items
    "slow"
    "fast"
    "faster"

    and holds the correct values for this enumeration

    Thnx
     
    Timothy Parez, Mar 3, 2004
    #1
    1. Advertising

  2. Timothy Parez

    Itai Raz Guest

    I don't believe there is (other than having a hash table or something like
    that, which stores the prospective enum value and string).

    --itai

    "Timothy Parez" <> wrote in message
    news:c25peh$fj$...
    > Hello,
    >
    > I have an enumeration for example
    >
    > enum Speed
    > {
    > slow,
    > fast,
    > faster
    > }
    >
    > Is there a way to bind this to a listbox ?
    > so that the listbox displays the items
    > "slow"
    > "fast"
    > "faster"
    >
    > and holds the correct values for this enumeration
    >
    > Thnx
     
    Itai Raz, Mar 5, 2004
    #2
    1. Advertising

  3. Timothy Parez

    ProJee Guest

    You're a lucky man:)

    I've made a complete example especially for you:)
    There are 3 ways to read it, specifying enum name by string, in code or
    by passing variable of the type (enum).



    Public Enum Speed
    NoMove
    Snail
    Normal
    Fast
    Faster
    SpeedOfLite
    End Enum

    Module SpeedyGonzales

    Public Sub GetEnumMembersByEnumName(ByVal EnumName$)
    Dim a%, iFieldName$, iFieldValue$

    '!!! must specify your Application name before the enum name!!!!!
    Dim iType As System.Type =
    System.Type.GetType("WindowsApplication2." & EnumName)
    Dim FI() As System.Reflection.FieldInfo = iType.GetFields()
    If FI.Length > 0 Then
    'dunno why, you have to start from 1 for enums, (but 0 for
    classes)
    For a = 1 To FI.Length - 1
    With FI(a)
    Dim iName$ = FI(a).Name
    End With
    Next a
    End If
    End Sub

    Public Sub GetEnumMembersByEnumName()
    Dim a%, iFieldName$, iFieldValue$

    'the type name is is directly in code, not so flexible.
    Dim iType As System.Type = GetType(Speed)
    Dim FI() As System.Reflection.FieldInfo = iType.GetFields()
    If FI.Length > 0 Then
    'dunno why, you have to start from 1 for enums, (but 0 for
    classes)
    For a = 1 To FI.Length - 1
    With FI(a)
    Dim iName$ = FI(a).Name
    End With
    Next a
    End If
    End Sub

    Private Sub GetEnumMembersByVariableOfEnum(ByVal Obj As Object)
    Dim a%, iFieldName$, iFieldValue$

    Dim iType As Type = Obj.GetType
    Dim FI() As System.Reflection.FieldInfo = iType.GetFields()

    If FI.Length > 0 Then
    'dunno why, you have to start from 1 for enums, (but 0 for
    classes)
    For a = 1 To FI.Length - 1
    With FI(a)
    Dim iName$ = FI(a).Name
    End With
    Next a
    End If
    End Sub

    Sub Main()
    'you can do it this way
    SpeedyGonzales.GetEnumMembersByEnumName("Speed")

    'or this way
    GetEnumMembersByEnumName()

    'or maybe this way...
    Dim m As Speed
    GetEnumMembersByVariableOfEnum(m)
    End Sub

    End Module


    Timothy Parez wrote:
    > Hello,
    >
    > I have an enumeration for example
    >
    > enum Speed
    > {
    > slow,
    > fast,
    > faster
    > }
    >
    > Is there a way to bind this to a listbox ?
    > so that the listbox displays the items
    > "slow"
    > "fast"
    > "faster"
    >
    > and holds the correct values for this enumeration
    >
    > Thnx
     
    ProJee, Apr 6, 2004
    #3
  4. Timothy Parez

    WhatIThink

    Joined:
    Dec 2, 2008
    Messages:
    9
    You can also inherit from the Listbox class.. (It will bind the enum automatically..)

    public class ListBoxEx : ListBox
    {
    public enum ObjectList
    {
    Block,
    Tile,
    Apple,
    }
    }
     
    WhatIThink, Jun 29, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olaf Petzold
    Replies:
    4
    Views:
    5,426
    Mike Treseler
    Nov 28, 2005
  2. tshad

    Dataset filling ListBox

    tshad, Oct 7, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    782
    tshad
    Oct 7, 2005
  3. puvit82
    Replies:
    4
    Views:
    762
    puvit82
    Feb 1, 2008
  4. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    121
    Mohsen Pahlevanzadeh
    Sep 9, 2013
  5. MRAB
    Replies:
    0
    Views:
    119
Loading...

Share This Page