C#MouseClick事件不会在中键单击或右键单击时触发

时间:2018-10-06 13:44:01

标签: c# right-click mouseclick-event

这似乎应该可以,但是不能。我在SWITCH语句上设置了调试停止。仅在单击鼠标左键时触发此事件。什么都没有发生,并且在中键或右键单击时未触发方法。有任何想法吗?附言我已经尝试使用MouseUp和MouseDown事件以及相同的问题。

这是我的代码:

this.textBox1.MouseClick +=
   new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseClick);

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

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

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

3 个答案:

答案 0 :(得分:10)

您需要使用MouseDown event来捕获鼠标中键和右键单击。 Click或MouseClick事件在管道中为时已晚,并被引用回文本框的默认OS上下文菜单行为。

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

答案 1 :(得分:4)

您只需要将该文本框的ShortcutsEnabled属性设置为False,并在MouseDown事件上编写代码。

它将起作用。

答案 2 :(得分:0)

您是否尝试过设置事件声明停止? 还可以通过单击鼠标中键进行测试

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

如果即使事件声明停止也没有触发事件,则说明项目有问题,请重新创建一个项目并进行测试。

相关问题