0

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?

4

4 回答 4

2

在组合上使用.text

Dim x As String = cmbFromGroup.text
于 2014-08-22T16:00:04.190 回答
1

我最终想出的解决方案是

cmbFromGroup.SelectedIndex = -1

然后在 cmbFromGroup_SelectedIndexChanged 子中捕获它。

于 2013-08-08T12:32:08.483 回答
1

我有同样的问题并设法使用数据库(使用存储过程)。这是代码的相关位

'On Form load or Button Click (Depending on your application)
conString.Open() 'change conString to your connection string name
'Set Command for the stored procedure
Dim sqlComGetDirectorates As New SqlCommand
With sqlComGetDirectorates
     .CommandType = CommandType.StoredProcedure
     .Connection = conString
     .CommandText = "ProcGetDirectorates" 'your stored procedure name
     'if your stored procedure has parameters
     '.Parameters.AddWithValue("@OfficerIdentity", txtPNum.Text) 
End With

'Create Data Adapter (Using the stored procedure Command created earlier)
' Create a data table and Fill the Data adapter 
Dim DatadptDirectorates As New SqlDataAdapter(sqlComGetDirectorates)
Dim dattabDirectorates As New DataTable
DatadptDirectorates.Fill(dattabDirectorates)
'Your Combobox 
ComboDirectorate.DataSource = dattabDirectorates
ComboDirectorate.DisplayMember = "DirectorateName"
ComboDirectorate.ValueMember = "DirectorateCode"
'User will see DisplayMember in the Combobox

'在组合框的 SelectedIndexChanged 上

txtDirectorate.Text = ComboDirectorate.SelectedItem(1)

''注意:Combobox.SelectedItem 将在内部从''存储过程'的结果中获取一个ROW,你需要通过指定combobox.selecteditem(??) ''在你的结果中(来自存储过程) 你有 0 - xx 列,为 (??) 选择适当的数字

希望这可以帮助。

于 2015-07-01T11:28:05.827 回答
0

在尝试将值绑定到ComboBox项目时,我遇到了类似的问题,我最终做的是:

Public Class Form1
    Dim ValuesList As List(Of String) = New List(Of String)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i = 1 To 10
            ComboBox1.Items.Add("Item" & i)
            ValuesList.Add("Value" & i)
        Next
    End Sub

    Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
        Dim selectedItem = ComboBox1.SelectedItem
        Dim selectedItemValue As String = ValuesList.Item(ComboBox1.SelectedIndex)
        MsgBox(selectedItem & " " & selectedItemValue)
    End Sub
End Class
于 2013-08-07T20:45:29.540 回答