Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何设置绑定到arraylist的组合框的vb.net selectedindex_Vb.net_Combobox - Fatal编程技术网

如何设置绑定到arraylist的组合框的vb.net selectedindex

如何设置绑定到arraylist的组合框的vb.net selectedindex,vb.net,combobox,Vb.net,Combobox,我已经搜索并找到了许多答案,但无法将特定的组合框项显示为在表单中显示记录的一部分 表单包含具有DropDownList样式的组合框。我完全按照描述使用ArrayList加载了它 加载组合框后,我正在读取一条数据库记录,我希望组合框的DisplayMember项显示的数据库值等于该项的ValueMember。我已经尝试了我能想到的一切,但是第一个DisplayMember项总是显示出来。我试过: cboVehicleBodyType.SelectedIndex = cboVehicleBodyTy

我已经搜索并找到了许多答案,但无法将特定的组合框项显示为在表单中显示记录的一部分

表单包含具有DropDownList样式的组合框。我完全按照描述使用ArrayList加载了它

加载组合框后,我正在读取一条数据库记录,我希望组合框的
DisplayMember
项显示的数据库值等于该项的
ValueMember
。我已经尝试了我能想到的一切,但是第一个
DisplayMember
项总是显示出来。我试过:

cboVehicleBodyType.SelectedIndex = cboVehicleBodyType.FindString(clsVehicle.fk_body_type)

cboVehicleBodyType.SelectedItem = clsVehicle.fk_body_type
…和其他几个变种,没有运气

组合框包含大约180种车身类型及其主键(从车身类型表加载),车辆的数据库记录包含车身类型主键(车辆记录中的外键)


如何获得要在组合框中显示的正确正文类型?

为了与MSDN中的示例保持一致,您可以选择如下项目:

  Private Sub SelectItem(box As ComboBox, stateKey As String)
    For curItem As Integer = 0 To box.Items.Count - 1
      If DirectCast(box.Items(curItem), USState).ShortName = stateKey Then
        box.SelectedIndex = curItem
        Return
      End If
    Next
  End Sub


  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim USStates As New ArrayList()
    USStates.Add(New USState("Alabama", "AL"))
    USStates.Add(New USState("Washington", "WA"))
    USStates.Add(New USState("West Virginia", "WV"))
    USStates.Add(New USState("Wisconsin", "WI"))
    USStates.Add(New USState("Wyoming", "WY"))
    ComboBox1.DataSource = USStates

    ComboBox1.DisplayMember = "LongName"
    ComboBox1.ValueMember = "ShortName"
    SelectItem(ComboBox1, "WY")
  End Sub
这段代码遍历元素并查找键值。FindString查找不会显示键的显示值