vb.net 组合框项目列表 SelectedValue

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18113052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:04:23  来源:igfitidea点击:

ComboBox ItemList SelectedValue

vb.netcomboboxdatasourcelist.selectedvalue

提问by Jane Alford

VB.NET (2010)

VB.NET (2010)

I'm trying to create a ComboBox with a Value and Display items. Here are the relevant bits of code. I originally tried to use a datatable with exactly the same result:

我正在尝试创建一个带有值和显示项的 ComboBox。这是相关的代码位。我最初尝试使用具有完全相同结果的数据表:

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

The problem I'm having is that cmbFromGroup.SelectedValue doesn't contain the value of the selected item.

我遇到的问题是 cmbFromGroup.SelectedValue 不包含所选项目的值。

What I'm getting is (from Watch) cmbFromGroup.SelectedValue {Player.SelectionItem} Object

我得到的是(来自 Watch) cmbFromGroup.SelectedValue {Player.SelectionItem} 对象

The only options I get from cmbFromGroup.SelectedValue are Equals,GetHashCode, GetType, ReferenceEquals and ToString, none of which give me the ValueMember as I'm expecting.

我从 cmbFromGroup.SelectedValue 获得的唯一选项是 Equals、GetHashCode、GetType、ReferenceEquals 和 ToString,它们都没有像我期望的那样给我 ValueMember。

How do I get the ValueMember from the selected item?

如何从所选项目中获取 ValueMember?

回答by Joseph Chalfoun

Use .texton the Combo:

.text在组合上使用:

Dim x As String = cmbFromGroup.text

回答by Isaac Njiru

I had the same issue and managed to use a Database (Using a Stored Procedure). Here are the relevant bits of Code

我遇到了同样的问题并设法使用了数据库(使用存储过程)。这是代码的相关部分

'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

'on SelectedIndexChanged of the Combobox

'在组合框的 SelectedIndexChanged 上

txtDirectorate.Text = ComboDirectorate.SelectedItem(1)

txtDirectorate.Text = ComboDirectorate.SelectedItem(1)

''NOTE: Combobox.SelectedItem will internally Get a ROW from the result of the ''Stored Procedure ''You need to obtain the value you need by specifying the combobox.selecteditem(??) ''In your result (from stored procedure) you have columns 0 - xx, chose the appropriate number for (??)

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

Hope this Helps.

希望这可以帮助。

回答by Jane Alford

The solution I eventually came up with is

我最终想出的解决方案是

cmbFromGroup.SelectedIndex = -1

Then trap for that in the cmbFromGroup_SelectedIndexChanged sub.

然后在 cmbFromGroup_SelectedIndexChanged 子中捕获它。

回答by Raphael Smit

I had a similar issue when trying to tie a value to a ComboBoxitem, What i ended up doing was something like:

我在尝试将值绑定到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