0

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

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

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

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

4

1 回答 1

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

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

谢谢!

于 2013-01-07T05:32:10.113 回答