单击ListBox1中的项目,同时在vb中同时选择ListBox2项目

时间:2015-05-28 00:21:10

标签: vb.net select listbox

基本上,当我有2个列表框时,当你在列表框中选择一个项目时,它会突出显示。

我想要做的是从任何列表框中选择一个项目,它会突出显示相应的数据。

这就是iv尝试过的,但显然这只是一个让程序崩溃的循环。

Private Sub lstOrders_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstOrders.SelectedIndexChanged
    lstTotalsEachOrder.SetSelected(lstOrders.SelectedIndex, True)
End Sub
Private Sub lstTotalsEachOrder_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstTotalsEachOrder.SelectedIndexChanged
    lstOrders.SetSelected(lstTotalsEachOrder.SelectedIndex, True)
End Sub

任何帮助将不胜感激:)

2 个答案:

答案 0 :(得分:3)

使用布尔标志。

Dim Selecting As Boolean = False
Private Sub lstOrders_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstOrders.SelectedIndexChanged
    If Not Selecting Then
       Selecting = True
       lstTotalsEachOrder.SetSelected(lstOrders.SelectedIndex, True)
       Selecting = False
    End If
End Sub
Private Sub lstTotalsEachOrder_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstTotalsEachOrder.SelectedIndexChanged
    If Not Selecting Then
       Selecting = True
       lstOrders.SetSelected(lstTotalsEachOrder.SelectedIndex, True)
       Selecting = False
    End If
End Sub

答案 1 :(得分:2)

只要留下一面旗帜就能知道你何时处理自己"选择

Private isManuallyHandled As Boolean = False

Private Sub ListBoxes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstOrders.SelectedIndexChanged, lstTotalsEachOrder.SelectedIndexChanged
    If isManuallyHandled Then Return

    Dim source = DirectCast(sender, ListBox)
    Dim target = If(source Is lstOrders, lstTotalsEachOrder, lstOrders)
    isManuallyHandled = True
    target.SelectedIndex = source.SelectedIndex ' simpler than SetSelected
    isManuallyHandled = False
End Sub