2011-11-24 99 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

哦,我的帖子之前没有得到更新,所以请放轻松,谢谢你的时间。 –

相关问题