Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need help to get the numeric value (constant) after = from an Enum by Index or Text from my ComboBox.

Thank you in advance,
Henrik

VB
Private Enum IPCamFrameRates
        <Description("Max")> Max = 0
        <Description("20")> f20 = 1
        <Description("15")> f15 = 3
        <Description("10")> f10 = 6
        <Description("5 frames")> f5 = 11
        <Description("4 frames")> f4 = 12
        <Description("3 frames")> f3 = 13
        <Description("2 frames")> f2 = 14
        <Description("1 frame")> f1 = 15
        <Description("1 fps/2s")> fps1_2s = 17
        <Description("1 fps/3s")> fps1_3s = 19
        <Description("1 fps/4s")> fps1_4s = 21
        <Description("1 fps/5s")> fps1_5s = 23
    End Enum
 
    Private Sub cboFrameRate1_SelectedIndexChanged(sender As System.Object, e _
      As System.EventArgs) Handles cboFrameRate1.SelectedIndexChanged
????? 
    End Sub
 
    Public Shared Function GetEnumDescription(ByVal EnumConstant As [Enum]) As _
      String
        Dim fi As Reflection.FieldInfo = EnumConstant.GetType().GetField( _
        EnumConstant.ToString())
        Dim attr() As System.ComponentModel.DescriptionAttribute = DirectCast( _
        fi.GetCustomAttributes(GetType( _
        System.ComponentModel.DescriptionAttribute), False), _
        System.ComponentModel.DescriptionAttribute())
        If attr.Length > 0 Then
            Return attr(0).Description
        Else
            Return EnumConstant.ToString()
        End If
    End Function
 
    Public Shared Function GetEnumDescriptions(ByVal EnumConstant As Type) As _
      String()
        ' SYNTAX : Me.cbFileActionType.Items.AddRange(GetEnumDescriptions( 
        ' GetType(eFileListActionType)))
        ' http://blog.dahead.de/howto-vb-net-enums-mit-beschreibungen-versehen
 
        Dim res(-1) As String
        For Each ec As [Enum] In [Enum].GetValues(EnumConstant)
            ReDim Preserve res(UBound(res) + 1)
            res(UBound(res)) = GetEnumDescription(ec)
        Next
        Return res
    End Function
Posted
Comments
[no name] 6-Sep-14 10:39am    
http://msdn.microsoft.com/en-us/library/essfb559(v=vs.110).aspx

1 solution

Instead of an Enum, you better use a Dictionary[^] for this. A dictionary is a collection of keys and values, so for example, you can use "5 frames" as key and 11 as value:

First, in your constructor, add the keys/values to the dictionary:
VB.NET
Public Class YourClass

    Private IPCamFrameRates As New Dictionary(Of String, Integer)()

    Public Sub New()
        IPCamFrameRates.Add("Max", 0)
        IPCamFrameRates.Add("20", 1)
        IPCamFrameRates.Add("15", 3)
        IPCamFrameRates.Add("10", 6)
        IPCamFrameRates.Add("5 frames", 11)
        ' add other values
    End Sub

And to get a value from the dictionary, try this:
VB.NET
If IPCamFrameRates.ContainsKey(yourKeyFromComboBox) Then
    Dim result As Integer = IPCamFrameRates(yourKeyFromComboBox)
Else
    ' given key not in dictionary
End If
 
Share this answer
 
v3
Comments
N. Henrik Lauridsen 6-Sep-14 10:58am    
Hi ProgramFOX, thank you for your fast reply. It looks good and I will try it right away.
Thomas Daniels 6-Sep-14 10:59am    
You're welcome!
N. Henrik Lauridsen 6-Sep-14 11:18am    
ProgramFOX you are the best it work just great.
I filled my combobox with the values from the Dictionary like this:
cboFrameRate1.DisplayMember = "Key"
cboFrameRate1.ValueMember = "Value"
cboFrameRate1.DataSource = New BindingSource(IPCamFrameRates, Nothing)

I hope this is correct.
Thank you again,
Henrik
Thomas Daniels 6-Sep-14 11:21am    
Yes, your code is correct.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900