VB2010。我正在尝试使用单位枚举的内容填充 ComboBox。我设法用字典做到了这一点。就像是
Dim dUnits As New Dictionary(Of String, Integer)
Dim da As String
For Each enumValue As eUnits In System.Enum.GetValues(GetType(eUnits))
da = ConvertEnumToCommonName 'gets unique name for an enumeration
dUnits.Add(da, enumValue)
Next
cbo.DisplayMember = "Key" 'display the the common name
cbo.ValueMember = "Value" 'use the enumeration as the value
cbo.DataSource = New BindingSource(dUnits, Nothing)
当我加载我的表单时效果很好。现在用户可以选择要显示的默认单位。那我试试
Dim defUnits As eUnits = eUnits.Feet
Dim idx As Integer = cbo.Items.IndexOf(defUnits) 'doesnt work, returns a -1
cbo.SelectedIndex = idx
我已经做了一段时间的研究,并且相当确定这与ComboBox
将值存储为字符串有关,实际上我正在寻找一个整数枚举。不知道我有没有这个权利。无论如何,我似乎无法选择默认项目。我可以尝试另一种方法吗?