使用C#在Windows窗体应用程序中正确使用OnClick与MouseClick事件

时间:2022-02-01 22:27:14

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.

我正在开发一个自定义控件,并意识到我的代码正在运行两次。这不是一个很大的问题(它只是一个Focus方法调用)。但是,我想了解它。

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

从阅读MSDN描述中点击| onclick事件,它声明:

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.

所以我添加了OnClick事件和MouseClick事件来处理左右点击。但在调试代码后,我发现OnClick处理左右键单击事件。

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

为什么OnClick处理这两个并且我是否需要将这两个事件保存在我的代码中由于某些原因我忽略了?

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);
   }
}

6 个解决方案

#1


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.

根据MSDN,Click事件不仅在单击鼠标时被调用,而且在按下Enter按钮时也被调用。如果您只需要处理鼠标点击,我会在MouseClick事件中移动您的所有代码。你不能反过来做因为Click事件不告诉你点击了哪个鼠标按钮(如果有的话)。

#2


First of all, your link is incorrect, it links to HTML and DHTML Reference, not WinForms :) Correct link is Control.MouseClick event
You need to override only one method. If you want to handle only mouse clicks - override OnMouseClick() and don't handle MouseClick event, otherwise - override OnClick() and don't override OnMouseClick().

首先,您的链接不正确,它链接到HTML和DHTML参考,而不是WinForms :)正确的链接是Control.MouseClick事件您只需要覆盖一个方法。如果您只想处理鼠标单击 - 覆盖OnMouseClick()并且不处理MouseClick事件,否则 - 覆盖OnClick()并且不要覆盖OnMouseClick()。

#3


You shouldn't need to have both events... Just keep the OnClick.

您不应该同时拥有这两个事件......只需保持OnClick即可。

Also, I haven't done Windows Forms in quite a while, but I think there's a better way to accept focus than manually setting it on the click event, but I can't tell you specifically what it is... I think there's a property for it or something.

此外,我还没有在很长一段时间内完成Windows Forms,但我认为有更好的方法来接受焦点,而不是在click事件上手动设置它,但我不能具体告诉你它是什么...我认为有它或某物的财产。

#4


In Winforms, the Click event is raised when either mouse key is clicked.

在Winforms中,单击任一鼠标键时会引发Click事件。

#5


If my memory serves me right, click does both mouseclick and the 'Enter' key or even setting focus on the control using the 'Tab' key and then using 'Space' or 'Enter' to "click" it.

如果我的记忆正确,请单击鼠标点击和“Enter”键,或者甚至使用“Tab”键将焦点设置在控件上,然后使用“Space”或“Enter”来“点击”它。

If such behaviour is acceptable/desired, you may do the following.

如果这种行为是可接受/期望的,您可以执行以下操作。

I had this workaround for a DoubleClick event...

我有一个DoubleClick事件的解决方法......

void ControlClick(object sender, EventArgs e)
{
    MouseEventArgs mEvt=e as MouseEventArgs; // or (MouseEventArgs)e;

    // now mEvt has the same properties as 'e' in MouseClick event
}

Hope this helps.

希望这可以帮助。

-Nurchi

#6


The OnClick and CustomControl_MouseClick is the same event

OnClick和CustomControl_MouseClick是同一个事件

You can have how many methods you want attached to an event ( this.Click += ...)

您可以将多少种方法附加到事件上(this.Click + = ...)

#1


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.

根据MSDN,Click事件不仅在单击鼠标时被调用,而且在按下Enter按钮时也被调用。如果您只需要处理鼠标点击,我会在MouseClick事件中移动您的所有代码。你不能反过来做因为Click事件不告诉你点击了哪个鼠标按钮(如果有的话)。

#2


First of all, your link is incorrect, it links to HTML and DHTML Reference, not WinForms :) Correct link is Control.MouseClick event
You need to override only one method. If you want to handle only mouse clicks - override OnMouseClick() and don't handle MouseClick event, otherwise - override OnClick() and don't override OnMouseClick().

首先,您的链接不正确,它链接到HTML和DHTML参考,而不是WinForms :)正确的链接是Control.MouseClick事件您只需要覆盖一个方法。如果您只想处理鼠标单击 - 覆盖OnMouseClick()并且不处理MouseClick事件,否则 - 覆盖OnClick()并且不要覆盖OnMouseClick()。

#3


You shouldn't need to have both events... Just keep the OnClick.

您不应该同时拥有这两个事件......只需保持OnClick即可。

Also, I haven't done Windows Forms in quite a while, but I think there's a better way to accept focus than manually setting it on the click event, but I can't tell you specifically what it is... I think there's a property for it or something.

此外,我还没有在很长一段时间内完成Windows Forms,但我认为有更好的方法来接受焦点,而不是在click事件上手动设置它,但我不能具体告诉你它是什么...我认为有它或某物的财产。

#4


In Winforms, the Click event is raised when either mouse key is clicked.

在Winforms中,单击任一鼠标键时会引发Click事件。

#5


If my memory serves me right, click does both mouseclick and the 'Enter' key or even setting focus on the control using the 'Tab' key and then using 'Space' or 'Enter' to "click" it.

如果我的记忆正确,请单击鼠标点击和“Enter”键,或者甚至使用“Tab”键将焦点设置在控件上,然后使用“Space”或“Enter”来“点击”它。

If such behaviour is acceptable/desired, you may do the following.

如果这种行为是可接受/期望的,您可以执行以下操作。

I had this workaround for a DoubleClick event...

我有一个DoubleClick事件的解决方法......

void ControlClick(object sender, EventArgs e)
{
    MouseEventArgs mEvt=e as MouseEventArgs; // or (MouseEventArgs)e;

    // now mEvt has the same properties as 'e' in MouseClick event
}

Hope this helps.

希望这可以帮助。

-Nurchi

#6


The OnClick and CustomControl_MouseClick is the same event

OnClick和CustomControl_MouseClick是同一个事件

You can have how many methods you want attached to an event ( this.Click += ...)

您可以将多少种方法附加到事件上(this.Click + = ...)