与在Windows鼠标点击事件的OnClick的正确使用表单中使用C#应用程序 [英] Correct usage of OnClick vs. MouseClick events in Windows Forms applications using C#

查看:200
本文介绍了与在Windows鼠标点击事件的OnClick的正确使用表单中使用C#应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个自定义的控制,实现了我的代码被运行两次。这是不是一个真正的大问题(它只是一个聚焦方法调用)。不过,我想了解它。



从阅读的 MSDN描述点击| onclick事件,它指出:




时触发用户点击的对象鼠标左键。




所以我添加OnClick事件和鼠标点击事件处理左,右点击。但调试代码后,我发现的OnClick处理左,右单击事件。



为什么的OnClick同时处理,做我需要保持这两个事件在我的代码由于某种原因,我俯瞰?



 保护覆盖无效的OnClick(EventArgs的发送)
{

this.Focus();
base.OnClick(E);
}

私人无效CustomControl_MouseClick(对象发件人,MouseEventArgs E)
{
如果(e.Button == MouseButtons.Right)
{
rightClickMenu(E);
}
}


解决方案

< A HREF =http://msdn.microsoft.com/en-us/library/system.windows.forms.control.click.aspx>根据MSDN ,Click事件被称为不仅当鼠标点击,而且当按下回车键。如果你只需要处理鼠标的点击,我把所有的代码的MouseClick事件。你不能做到这一点的其他方式,因为Click事件不会告诉你的鼠标按钮(如果有的话)被单击。


I'm currently developing a custom control and realize that my code is being run twice. It is not really a huge issue (it is only a Focus method call). However, I would like to understand it.

From reading the MSDN description for click | onclick event, it states that:

Fires when the user clicks the left mouse button on the object.

So I added the OnClick event and the MouseClick events to handle both left and right clicking. But after debugging the code I found that the OnClick handles both left and right click events.

Why is OnClick handling both and do I need to keep both events in my code for some reason I'm overlooking?

protected override void OnClick(EventArgs e)
{

   this.Focus();
   base.OnClick(e);
}

private void CustomControl_MouseClick(object sender, MouseEventArgs e)
{       
   if (e.Button == MouseButtons.Right)
   {
      rightClickMenu(e);
   }
}

解决方案

According to MSDN, the Click event is called not only when the mouse is clicked, but also when the Enter button is pressed. If you only need to handle mouse clicks, I'd move all of your code in the MouseClick event. You can't do it the other way around because the Click event doesn't tell you which mouse button (if any) was clicked.

这篇关于与在Windows鼠标点击事件的OnClick的正确使用表单中使用C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆