在组合框上显示枚举(vb.net)
我试图只显示vb.net上combobox上enum中的数字 这是我的枚举在组合框上显示枚举(vb.net),.net,vb.net,winforms,.net,Vb.net,Winforms,我试图只显示vb.net上combobox上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
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
这是我的组合框
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
End Sub
所以我想让下拉列表保存显示的州代码,但值应该是一个数字,例如,对于内华达州,它应该显示NV,ID是29,当用户选择一个州时,类不应该保存州字母,而是ID
提前谢谢你 试试这个
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
编辑:这将清除名称并对状态缩写进行排序
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
导入System.ComponentModel
公开课表格1
公共枚举状态
钠=0
AK=1
铝=2
氩=3
AZ=4
钙=5
一氧化碳=6
CT=7
德鲁=8
FL_uu9
镓=10
你好,11岁
IA=12
ID=13
白细胞介素=14
IN=15
KS=16
KY=17
LA_uuz=18
马=19
MD=20
我=21
米=22
锰=23
MO=24
MS=25
MT=26
NC=27
钕=28
NE=29
NH=30
NJ=31
纳米=32
内华达州=33
纽约=34
哦,35岁
OK=36
或=37
帕=38
RI=39
SC=40
SD=41
TN=42
TX=43
UT=44
VA=45
VT=46
WA=47
WI=48
WV_uu=49
WY=50
结束枚举
显示的私有子表单1_(发送者作为对象,e作为事件参数)处理我。显示
朦胧的
ie=[Enum].GetValues(GetType(States)).AsQueryable
对于ie中的每个i状态
ComboBox1.Items.Add(GetEnumDescription(i))
下一个
端接头
公共函数GetEnumDescription(EnumConstant作为[Enum])作为字符串
Dim attr()作为DescriptionAttribute=DirectCast(EnumConstant.GetType().GetField(EnumConstant.ToString()).GetCustomAttributes(GetType(DescriptionAttribute),False),DescriptionAttribute())
返回If(attr.Length>0,attr(0).Description,EnumConstant.ToString)
端函数
私有子Combox1\u SelectedIndexChanged(发送方作为对象,e作为事件参数)处理Combox1.SelectedIndexChanged
Dim ID为整数'=ComboBox1.SelectedIndex'???
ID=DirectCast([Enum].Parse(GetType(States),ComboBox1.SelectedItem.ToString&“\”),整数)
端接头
末级
由于每个状态的数值是按数字顺序排列的,因此您只需获取所选项目的索引即可。
您只需创建一个数组,重新组合枚举的所有值,将其显示在组合框中,并获取所选组合框项的索引:
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
这回答了你的问题吗?这回答了你的问题吗?感谢您抽出时间回答!但是我们可以在组合框中显示ID,而不是在messagebox中显示ID吗?当我选择一个状态时,只选择字母。当然,请检查我对答案的编辑!