同步三个组合框

时间:2013-01-07 01:31:26

标签: vb.net

我有3个组合框从数据库加载但没有绑定,具有不同的数据但索引相同 所有这些都设置如下:

 ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest
 ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
 ComboBox1.AutoCompleteCustomSource = mycolumn1
 ComboBox1.DropDownStyle = DropDownList

我想获得的功能是,当我在一个组合中选择一个项目时,其他两个选择具有相同索引的项目。 敌人开始我非常惊讶_SelectedIndexChanged从未被触发,而我期望从那里得到索引。

为什么会这样以及如何获得所需的功能?

1 个答案:

答案 0 :(得分:1)

我不确定您的问题部分原因是因为您没有发布任何代码可以帮助您解决问题。这是我为你做的一个例子。这是一个快速的,但有效;你可以在一个程序中实际完成这个,但这样做是为了让你能够理解它的工作原理。

    Public Class Form1

'Always give variable a default value'
Private selectedIndex As Integer = 0

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim myArray() As String = {"1", "2", "3"}
    ComboBox1.Items.AddRange(myArray)
    ComboBox2.Items.AddRange(myArray)
    ComboBox3.Items.AddRange(myArray)
End Sub

'Handles one of your comboboxes'
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    'Cast this as Integer for selected index and set your variable'
    selectedIndex = CType(ComboBox1.SelectedIndex.ToString, Integer)

    'Next lets make sure that we set the other comboboxes to this index'
    ComboBox2.SelectedIndex = selectedIndex
    ComboBox3.SelectedIndex = selectedIndex
End Sub

'Another one of your comboboxes'
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
    'Cast this as Integer for selected index and set your variable'
    selectedIndex = CType(ComboBox2.SelectedIndex.ToString, Integer)

    'Next lets make sure that we set the other comboboxes to this index'
    ComboBox1.SelectedIndex = selectedIndex
    ComboBox3.SelectedIndex = selectedIndex
End Sub

'Your last combobox'
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
    'Cast this as Integer for selected index and set your variable'
    selectedIndex = CType(ComboBox3.SelectedIndex.ToString, Integer)

    'Next lets make sure that we set the other comboboxes to this index'
    ComboBox1.SelectedIndex = selectedIndex
    ComboBox2.SelectedIndex = selectedIndex
End Sub
    End Class

* 您必须将全局变量添加到顶部,以便它可用于保存当前组合框选定的索引。您也可以忽略load事件,因为我将其用作参考。

谢谢!