2015-05-28 79 views
-1

基本上,當我有2個列表框,當你選擇列表框中的項目時,它會被突出顯示。單擊列表框1中的項目和列表框2項目也同時選擇在vb

我希望能夠做的是從任何列表框中選擇一個項目,並突出顯示相應的數據。

這是iv'e的嘗試,但顯然這只是一個循環崩潰的程序。

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 

任何幫助,將不勝感激:)

+0

你可以設置控制到'SetSelected'調用一個布爾值。 –

+0

@ DanielA.White對不起,你能告訴我該怎麼做 – IeuanW

+1

正如我在你對上一個問題的評論中所提到的,如果你使用了一個ListView,那麼所有相關的數據都將在不同的子項中相同的項目。 – Blackwood

回答

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 
+0

多數民衆贊成真棒謝謝人 – IeuanW

+0

我發現一個問題,如果我嘗試刪除項目使用下面的代碼lstOrders.Items.RemoveAt(lstOrders.Items.Count - 1),lstOrders.Items.RemoveAt(lstOrders.Items.Count - 1) 。它的罰款,直到它到達一個選定的項目和錯誤拋出的代碼 – IeuanW

+0

@leuan你應該問一個單獨的問題在這種情況下 –

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 
+0

謝謝這工程以及 – IeuanW

相關問題