3 Replies - 496 Views - Last Post: 28 October 2009 - 11:16 AM

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Bind a control to an enumeration

Posted 18 August 2007 - 07:31 PM

Description: This is a function to bind a control to an enumeration by taking the values in the enumeration and adding them to an ArrayList

Create your enumeration

Enum
  Value_1 = 1
  Value_2 = 2
...
End Enum


Then call the function when wanting to bind to an enumeration


''' <summary>
''' Function to bind a control to an enumeration
''' </summary>
''' <param name="_enum">The enum with the Names/Values to show</param>
''' <returns>An ArrayList with the name/value pairs of an enukeration</returns>
''' <remarks></remarks>
Public Shared Function BindEnumeration(ByVal _enum As Type) As ArrayList
    'get the names from the enumeration
    Dim enumNames() As String = System.Enum.GetNames(_enum)
    'get the values from the enumeration
    Dim enumValues() As Integer = System.Enum.GetValues(_enum)
    'turn it into a hash table
    Dim aList As New ArrayList()
    Dim enumList As New ArrayList()
    For i As Integer = 0 To enumNames.Length - 1
        aList.Insert(enumValues(i), enumNames(i))
    Next
    Return enumList
End Function

'Sample useage
DropDownList1.DataSource = BindEnumeration(GetType(YourEnum))

This post has been edited by AdamSpeight2008: 11 January 2015 - 07:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Bind a control to an enumeration

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Bind a control to an enumeration

Posted 18 August 2007 - 07:31 PM

Description: Create your enumeration

Enum
Value_1 = 1
Value_2 = 2
...
End Enum

Then call the function when wanting to bind to an enumerationThis is a function to bind a control to an enumeration by taking the values in the enumeration and adding them to an ArrayList
''' <summary>
''' Function to bind a control to an enumeration
''' </summary>
''' <param name="_enum">The enum with the Names/Values to show</param>
''' <returns>An ArrayList with the name/value pairs of an enukeration</returns>
''' <remarks></remarks>
Public Shared Function BindEnumeration(ByVal _enum As Type) As ArrayList
    'get the names from the enumeration
    Dim enumNames() As String = System.Enum.GetNames(_enum)
    'get the values from the enumeration
    Dim enumValues() As Integer = System.Enum.GetValues(_enum)
    'turn it into a hash table
    Dim aList As New ArrayList()
    For i As Integer = 0 To enumNames.Length - 1
        aList.Insert(enumValues(i), enumNames(i))
    Next
    Return aList
End Function

'Sample useage
DropDownList1.DataSource = BindEnumeration(GetType(YourEnum))

This post has been edited by AdamSpeight2008: 11 January 2015 - 07:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 jschram   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 28-October 09

Re: Bind a control to an enumeration

Posted 28 October 2009 - 11:13 AM

This won't work. See corrected code immediately below:

    ''' <summary>
    ''' Function to bind a control to an enumeration
    ''' </summary>
    ''' <param name="_enum">The enum with the Names/Values to show</param>
    ''' <returns>An ArrayList with the name/value pairs of an enukeration</returns>
    ''' <remarks></remarks>
    Public Shared Function BindEnumerationTextOnly(ByVal _enum As Type) As ArrayList
        'get the names from the enumeration
        Dim enumNames() As String = System.Enum.GetNames(_enum)
        'get the values from the enumeration
        Dim enumValues() As Integer = System.Enum.GetValues(_enum)
        'turn it into a hash table
        Dim enumList As New ArrayList()
        For i As Integer = 0 To enumNames.Length - 1
            'enumList.Add(New KeyValuePair(Of Integer, String)(enumValues(i), enumNames(i)))
            enumList.Insert(enumValues(i), enumNames(i))
        Next
        Return enumList
    End Function

This post has been edited by AdamSpeight2008: 11 January 2015 - 07:31 PM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Bind a control to an enumeration

Posted 28 October 2009 - 11:16 AM

Considering that I took this code from a working application I would say that it does work :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1