簡體   English   中英

C#MouseClick事件不會在中鍵單擊或右鍵單擊時觸發

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

這似乎應該有效,但事實並非如此。 我在SWITCH語句中設置了一個調試停止。 此事件僅在左鍵單擊時觸發。 沒有任何反應,並且在中間或右鍵單擊時不會觸發方法。 有任何想法嗎? PS我已經嘗試過使用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;
    }
}

您需要使用MouseDown事件來捕獲鼠標中鍵和右鍵。 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;
    }
}

您只需要為該文本框將ShortcutsEnabled屬性設置為False,並在MouseDown事件上編寫代碼。

它會工作。

您是否嘗試過在事件聲明中設置停止? 也可以點擊鼠標中鍵進行測試

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

如果事件未在事件聲明中停止時觸發,則項目出現問題,創建一個新項目並進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM