简体   繁体   中英

Display enum on combobox (vb.net)

I am trying to display only the numbers from enum on combobox on vb.net

Here is my enum

Public Enum States
    
    NA = 0
    AL = 1
    AK = 2
    AZ = 3
    AR = 4
    CA = 5
    CO = 6
    CT = 7
    DE = 8
    FL = 9
    GA = 10
    HI = 11
    ID = 12
    IL = 13
    [IN] = 14
    IA = 15
    KS = 16
    KY = 17
    LA = 18
    [ME] = 19
    MD = 20
    MA = 21
    MI = 22
    MN = 23
    MS = 24
    OK = 25
    MO = 26
    MT = 27
    NE = 28
    NV = 29
    NH = 30
    NJ = 31
    NM = 32
    NY = 33
    NC = 34
    ND = 35
    OH = 36
    [OR] = 37
    PA = 38
    RI = 39
    SC = 40
    SD = 41
    TN = 42
    TX = 43
    UT = 44
    VT = 45
    VA = 46
    WA = 47
    WV = 48
    WI = 49
    WY = 50
End Enum

and here is my combobox

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

End Sub

So I wanna make the dropdown hold the state code that is displayed but the value should be a number, for instance, for Nevada, it should display NV, and ID is 29, and when the user selects a state, the class should not hold the state letters but the ID.

Thank you in advance!

Try this

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    ComboBox1.Items.AddRange([Enum].GetNames(GetType(States)))
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim ID As Integer
    ID = DirectCast([Enum].Parse(GetType(States), ComboBox1.SelectedItem.ToString), Integer)
End Sub

edit: This cleans up the names and has the State abbreviations sorted.

Imports System.ComponentModel

Public Class Form1
    Public Enum States
        <Description("NA")> NA_ = 0
        <Description("AK")> AK_ = 1
        <Description("AL")> AL_ = 2
        <Description("AR")> AR_ = 3
        <Description("AZ")> AZ_ = 4
        <Description("CA")> CA_ = 5
        <Description("CO")> CO_ = 6
        <Description("CT")> CT_ = 7
        <Description("DE")> DE_ = 8
        <Description("FL")> FL_ = 9
        <Description("GA")> GA_ = 10
        <Description("HI")> HI_ = 11
        <Description("IA")> IA_ = 12
        <Description("ID")> ID_ = 13
        <Description("IL")> IL_ = 14
        <Description("IN")> IN_ = 15
        <Description("KS")> KS_ = 16
        <Description("KY")> KY_ = 17
        <Description("LA")> LA_ = 18
        <Description("MA")> MA_ = 19
        <Description("MD")> MD_ = 20
        <Description("ME")> ME_ = 21
        <Description("MI")> MI_ = 22
        <Description("MN")> MN_ = 23
        <Description("MO")> MO_ = 24
        <Description("MS")> MS_ = 25
        <Description("MT")> MT_ = 26
        <Description("NC")> NC_ = 27
        <Description("ND")> ND_ = 28
        <Description("NE")> NE_ = 29
        <Description("NH")> NH_ = 30
        <Description("NJ")> NJ_ = 31
        <Description("NM")> NM_ = 32
        <Description("NV")> NV_ = 33
        <Description("NY")> NY_ = 34
        <Description("OH")> OH_ = 35
        <Description("OK")> OK_ = 36
        <Description("OR")> OR_ = 37
        <Description("PA")> PA_ = 38
        <Description("RI")> RI_ = 39
        <Description("SC")> SC_ = 40
        <Description("SD")> SD_ = 41
        <Description("TN")> TN_ = 42
        <Description("TX")> TX_ = 43
        <Description("UT")> UT_ = 44
        <Description("VA")> VA_ = 45
        <Description("VT")> VT_ = 46
        <Description("WA")> WA_ = 47
        <Description("WI")> WI_ = 48
        <Description("WV")> WV_ = 49
        <Description("WY")> WY_ = 50
    End Enum

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Dim ie As IQueryable
        ie = [Enum].GetValues(GetType(States)).AsQueryable
        For Each i As States In ie
            ComboBox1.Items.Add(GetEnumDescription(i))
        Next
    End Sub

    Public Function GetEnumDescription(EnumConstant As [Enum]) As String
        Dim attr() As DescriptionAttribute = DirectCast(EnumConstant.GetType().GetField(EnumConstant.ToString()).GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
        Return If(attr.Length > 0, attr(0).Description, EnumConstant.ToString)
    End Function

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim ID As Integer '= ComboBox1.SelectedIndex '???
        ID = DirectCast([Enum].Parse(GetType(States), ComboBox1.SelectedItem.ToString & "_"), Integer)
    End Sub
End Class

Since your the number value of each State is in numeric order, you can just get the index of the selected item. You just have to create an array regrouping all the values of your Enum, display it in your ComboBox and get the index of the selected ComboBox item:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Create an array and put the values in it:
    Dim items = System.Enum.GetNames(GetType(States)) 'To put the letters in the array
    'Dim items = System.Enum.GetValues(GetType(States)) 'To put the numbers in the array

    'Display the enum items in the combobox:
    Dim item As String
    For Each item In items
        ComboBox1.Items.Add(item)
    Next
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    'To get the number of the selected item
    Dim valueSelected As Integer = ComboBox1.SelectedIndex

    If IsNumeric(ComboBox1.SelectedItem) = False Then
        If IsNumeric(ComboBox1.Items.IndexOf(ComboBox1.Items.Count - 1)) Then
            ComboBox1.Items.RemoveAt(ComboBox1.Items.Count - 1)
        End If
        ComboBox1.Items.Add(valueSelected)

        'Select the value that we just added.
        ComboBox1.SelectedIndex = ComboBox1.Items.Count - 1
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM