【发布时间】:2012-03-01 19:40:36
【问题描述】:
让我试着用最简单的方式描述我的问题:我有combobox1 和combobox2。我希望实现两件事:
Combox1 绑定到 list1(字符串列表)。当用户选择 list1 中的一个项目时,将从数据库中获取 list2(字符串列表)并将组合框绑定到 list2。
如果用户在 combobox1 中指定 text1,在 combobox2 中指定 text2,则这两个值将显示在组合框中,无论绑定列表如何。
所以我将 DropDown 设置为两个组合框的 dropdpwnstyle。
Public Sub New(Optional ByVal text1 As String = "", Optional ByVal text2 As String = "")
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.combobox1.selectedText=text1
Me.combobox2.selectedText=text2
End Sub
Private Sub Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load
BindComboBox1()
End Sub
Private Sub BindComboBox1()
'm_list1 is a list of string
combobox1.DataSource = m_list1
End Sub
Private Sub GetCombobox2()
'based on the selected item in combobox1, m_list2 which is a list of string is obtained
ComboBox2.DataSource = m_list2
End Sub
Private Sub combobox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles combobox1.SelectedIndexChanged
If combobox1.SelectedIndex <> -1 Then
GetCombobox2()
End If
End Sub
当我调试时,我注意到两件事:
在 Me.combobox1.SelectedText=text1 之后,实际上是 Me.combobox1.SelectedText=""。但是 Me.combobox1.Text=text1。这是因为 combobox1.SelectedIndex=-1 吗?
Combobox1.datasource=m_list1 将 combobox1.selectedindex 从 -1 更改为 0。这将触发 combobox.selectedIndexchange 事件。
所以上面代码的结果是目标 1 实现了,但目标 2 永远没有实现。 combobox1.selected 索引始终为 0,combobox2.selected 索引也始终为 0。
【问题讨论】: