C# MouseClick Event Doesn't Fire On Middle Click or Right Click

时间秒杀一切 提交于 2019-12-05 05:47:33

You need to use the MouseDown event to trap Middle and Right mouse clicks. The Click or MouseClick events are too late in the pipeline and are referred back to default OS Context Menu behaviour for textboxes.

private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
    switch (e.Button)
    {
        case MouseButtons.Left:
            // Left click
            txt.Text = "left";
            break;

        case MouseButtons.Right:
            // Right click
            txt.Text = "right";
            break;

        case MouseButtons.Middle:
            // Middle click
            txt.Text = "middle";
            break;
    }
}

You only need to set the attribute ShortcutsEnabled to False for that Textbox and write your code on MouseDown event.

It will work.

Have you tried setting the stop on event declaration? Also test this with middle mouse button click

if e.Button = 4194304 Then
    a = b //set the stop here
End if

If the event is not triggering even with the stop on the event declaration, something is wrong with the project, make a new one and test.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!