2011-11-24 97 views
1

我對Winform開發頗爲新穎。我有兩個列表框。當用戶雙擊第一個列表框中的一個項目時,我想將其複製到第二個列表框中。問題是我的雙擊方法從未被解僱。 這裏是我的代碼:雙擊將項目從一個列表框複製到另一個列表框。 Doubleclick事件未觸發。 Winform C#

//here I register the event 
this.fieldsArea.MouseDoubleClick += new MouseEventHandler(fieldsArea_MouseDoubleClick); 

那麼這裏就是雙擊方法:

private void fieldsArea_MouseDoubleClick(object sender, MouseEventArgs e) 
    { 
     MessageBox.Show("from method"); 
     int index = fieldsArea.IndexFromPoint(e.Location); 
     string s = fieldsArea.Items[index].ToString(); 

     selectedFieldsArea.Items.Add(s); 
    } 

所以我想從fieldsArea元素被複制到selectedFieldsArea ......這些URL從未顯示和調試我看到我從來沒有進入這種方法... 我在這裏錯過了什麼?

ps:我已經拖放執行,效果很好。

UPDATE:問題來自同時正在實施的MouseDown事件。所以這是我的ousedown事件。

private void fieldsArea_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (fieldsArea.Items.Count == 0) 
      return; 
     int index = fieldsArea.IndexFromPoint(e.Location); 
     string s = fieldsArea.Items[index].ToString(); 
     DragDropEffects dde1 = DoDragDrop(s, 
      DragDropEffects.All); 
    } 

回答

1

確保你沒有其他的鼠標事件像註冊MouseClickMouseDown事件,這可能與MouseDoubleclick事件干擾。

更新:在您的MouseDown事件處理程序

添加以下代碼,您可以檢查它是否是第一雙擊。

if(e.Clicks>1) 
{ 
    int index = fieldsArea.IndexFromPoint(e.Location); 
    string s = fieldsArea.Items[index].ToString(); 
    selectedFieldsArea.Items.Add(s); 
} 

所以這裏是新的處理程序:

private void fieldsArea_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (fieldsArea.Items.Count == 0) 
      return; 
    int index = fieldsArea.IndexFromPoint(e.Location); 
    string s = fieldsArea.Items[index].ToString(); 

    if(e.Clicks>1) 
    {   
     selectedFieldsArea.Items.Add(s); 
    } 
    else 
    { 
     DragDropEffects dde1 = DoDragDrop(s, 
     DragDropEffects.All); 
    } 
} 
+0

我做我的拖放...是不是可以同時拖放和雙擊? ps:我剛剛評論了我的mousedown,它解決了這個問題... – nche

+0

@nche,這是可能的,只是張貼你的mousedown事件,我們可以爲你找一個。 – Bolu

+0

我只是將它添加到問題 – nche

2

PS:我已經實現拖放效果很好。

這意味着您可能註冊了一個MouseDown事件,這會干擾MouseDoubleclick

僅用於測試目的,嘗試刪除拖放實施(取消註冊MouseDown事件),然後MouseDoubleclick應該工作。

+0

我做了,它的工作原理。所以我想我會更新我的問題,問如何實現這兩個。 – nche

0

我相信你可能有「MouseClick/MouseDown」事件或「SelectedIndexChanged」事件,這些事件抵制得到「MouseDoubleclick」事件的火焰,所以你需要正確處理它們。謝謝

+0

哦,我的帖子之前沒有得到更新,所以請放輕鬆,謝謝你的時間。 –

相關問題