我是Winform dev的新手。我有两个列表框。当用户双击第一个列表框中的项目时,我希望将其复制到第二个列表框中。问题是我的双击方法永远不会被触发。 这是我的代码:
//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 ...消息框永远不显示,在调试中我看到我从未输入此方法... 我在这里错过了什么吗?
ps:我实施了拖拽效果很好。
UPDATE :问题来自同时正在实施的MouseDown事件。所以这是我的mousedown活动。
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);
}
答案 0 :(得分:2)
ps:我实施了拖拽效果很好。
这意味着您可能已注册MouseDown
事件,该事件会干扰MouseDoubleclick
。
仅出于测试目的,尝试删除Drag& Drop实现(取消注册MouseDown
事件),然后MouseDoubleclick
应该有效。
答案 1 :(得分:1)
确保您没有注册MouseClick
MouseDown
个事件等其他鼠标事件,这可能会干扰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);
}
}
答案 2 :(得分:0)
我相信你可能有“MouseClick / MouseDown”事件或“SelectedIndexChanged”事件,这些事件无法触发“MouseDoubleclick”事件,所以你需要正确处理它们。感谢