2011-09-21 28 views
0

我有兩個列表框,一個用於可用項目和一個用於選定項目,因此用戶可以通過拖放或雙擊鼠標在這兩者之間移動項目,單擊。 這不是一個完美的代碼,大部分的邏輯都是在MouseMove事件中寫入的,我可以在鼠標位置放置X和Y ...我正在尋找這種情況來防止它:用戶在左邊拿着鼠標左鍵列表框並選擇一個項目,但他在同一個列表框中再次釋放鼠標按鈕,所以我需要一種方法來知道它是否仍然在同一個列表框中,然後不要拖放...所以有一些方法可以告訴我可以使用的列表框的邊界嗎?或者你有更好的想法?知道鼠標左鍵是否釋放在同一個列表框中

private void lstAvailable_MouseMove(Object eventSender, MouseEventArgs eventArgs) 
{ 
    //*************** FUNCTION DETAILS *************** 
    //User moves mouse in the Available list 
    //***************** INSTRUCTIONS ***************** 
    MouseButtons Button = eventArgs.Button; 
    int Shift = (int)Control.ModifierKeys/0x10000; 
    float X = (float)VB6.PixelsToTwipsX(eventArgs.X); 
    float Y = (float)VB6.PixelsToTwipsY(eventArgs.Y); 
    moDualListBox.List1_MouseMove(Button, Shift, X, Y); 


    if (eventArgs.Button == MouseButtons.Left) 
    { 
     if (!mbClickProcessed) // it is a DragDrop 
     { 
      this.lstAvailable.DoDragDrop(this.lstAvailable.SelectedItems, DragDropEffects.Move); 
      mbClickProcessed = true; 
     } 
     if (mbClickProcessed) // it is a DoubleClick 
     { 
      MoveClick(); 
      MoveLostFocus(); 
      mbClickProcessed = true; 
     } 
    } 
} 

回答

0

如果您捕獲了DragDrop事件,則您有拖放操作的發送者。
所以,你可以輕易放棄的操作(或者什麼都不做)發件人是否相同目的地控制...

如果你想知道,如果鼠標離開控制(根據這個設置一個變量),你可以捕獲MouseLeave事件,而MouseEnter事件便於知道鼠標何時從另一個控件進入控件。

+0

沒有工作。我很快就會把MouseMove邏輯放在我的問題中,這種邏輯寫得不夠好,不足以處理這個問題。 – Bohn

+0

我們可以看到你用來試用我的解決方案的代碼(和@LarsTech之一,在這裏也有描述)嗎?它真的很奇怪,不起作用... – Marco

+0

當然。我用代碼更新了我的問題。 – Bohn

1

樣品進行拖放(沒有錯誤檢查):

private ListBox _DraggingListBox = null; 

private void listBox1_DragDrop(object sender, DragEventArgs e) 
{ 
    if (_DraggingListBox != listBox1) 
    MoveItem(listBox2, listBox1, (int)e.Data.GetData(typeof(int))); 
} 

private void listBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    _DraggingListBox = listBox1; 
    listBox1.DoDragDrop(listBox1.IndexFromPoint(e.X,e.Y), DragDropEffects.Move);  
} 

private void listBox1_DragOver(object sender, DragEventArgs e) 
{ 
    e.Effect = DragDropEffects.Move; 
} 

private void listBox2_DragDrop(object sender, DragEventArgs e) 
{ 
    if (_DraggingListBox != listBox2) 
    MoveItem(listBox1, listBox2, (int)e.Data.GetData(typeof(int))); 
} 

private void listBox2_DragOver(object sender, DragEventArgs e) 
{ 
    e.Effect = DragDropEffects.Move; 
} 

private void listBox2_MouseDown(object sender, MouseEventArgs e) 
{ 
    _DraggingListBox = listBox2; 
    listBox2.DoDragDrop(listBox2.IndexFromPoint(e.X,e.Y), DragDropEffects.Move); 
} 

private void MoveItem(ListBox fromLB, ListBox toLB, int index) 
{ 
    toLB.Items.Add(fromLB.Items[index]); 
    fromLB.Items.RemoveAt(index); 
} 
+0

沒有工作。我很快就會把MouseMove邏輯放在我的問題中,這種邏輯寫得不夠好,不足以處理這個問題。 – Bohn

+1

@BDotA DragDrop應該從MouseDown事件開始,而不是MouseMove事件。您也應該在DoubleClick活動中處理DoubleClick。 – LarsTech

+0

是的,我知道。但它是從VB 6.0更新到C#的遺留代碼,也是其他團隊使用的可重用組件,我只是在尋找一種快速和骯髒的方法來修復它在當前代碼中。在這個邏輯中,我認爲如果對於拖放部分,我檢查鼠標所在的位置和列表框的邊界,然後我可以確定它在同一個列表框中,因此不要執行拖放操作。 。所以我的問題是我該怎麼做? – Bohn

相關問題