VB.NET (2010)
我正在尝试创建一个带有值和显示项的组合框。这是相关的代码位。我最初尝试使用结果完全相同的数据表:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Try
cmbFromGroup.Items.Clear()
ItemList.Clear()
Item = New SelectionItem(KeyValue, DisplayValue)
'Link combobox and Item
ItemList.Add(Item)
cmbFromGroup.DataSource = ItemList
cmbFromGroup.DisplayMember = "Display"
cmbFromGroup.ValueMember = "Key"
Catch ex As Exception
Stop
End Try
End Sub
Private Sub cmbFromGroup_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbFromGroup.SelectedIndexChanged
Try
cmbFromMP3.Items.Clear()
Dim x As String = cmbFromGroup.SelectedValue
' <snip>
cmbFromMP3.SelectedIndex = 0
Catch ex As Exception
Stop
End Try
End Sub
Public Interface ISelectionItem
Property Key As String
Property Display As String
End Interface
Public Class SelectionItem
Implements ISelectionItem
Public Sub New(ByVal vKey As String, ByVal vDisplay As String)
_Key = vKey
_Display = vDisplay
End Sub
Public Property Key As String Implements ISelectionItem.Key
Public Property Display As String Implements ISelectionItem.Display
End Class
我遇到的问题是 cmbFromGroup.SelectedValue 不包含所选项目的值。
我得到的是(从手表) cmbFromGroup.SelectedValue {Player.SelectionItem} 对象
我从 cmbFromGroup.SelectedValue 获得的唯一选项是 Equals、GetHashCode、GetType、ReferenceEquals 和 ToString,它们都没有给我预期的 ValueMember。
如何从所选项目中获取 ValueMember?