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

3

9

This seems like it it should work but its not. I put a debug stop on the SWITCH statement. This event is only getting triggered on left click. Nothing happens and method is not fired on middle or right click. Any ideas? P.S. I already tried using MouseUp and MouseDown events and same issue.

Here is my code:

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;
    }
}
Tillion answered 6/10, 2018 at 13:44 Comment(6)
Use the textBox1_MouseDown event instead ! This works fine here; to avoid the context menue popping up you may want to disable ShortCuts. Btw: It is recommended to test a mousebutton like this: if (e.Button.HasFlag(MouseButtons.Left)).. etc..Durgy
@Durgy The first thing I did before I posted here was trying MouseDown and MouseUp. I have the same issue. Putting in a break point and the method is never fired on middle or left click.Tillion
I know you tried as you wrote it. But it still works here. Where do you put the breakpoint? The switch statement?Durgy
@Durgy Break point is on the switch lineTillion
This is by design for the TextBox and RichTextBox classes. Note the test for MouseButtons.Left. I'm not that sure why this was deemed necessary, it might have something to do with the built-in context menu in the native OS control. It is fixable by deriving your own class from TextBox and overriding OnMouseUp(), now omitting the test on the button. At which point it is likely you'll discover why it was deemed necessary :)Garica
I know this may seem silly but have you tried : this.textBox1.MouseDown += ...Lifeanddeath
C
13

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;
    }
}
Curler answered 12/11, 2018 at 4:0 Comment(4)
Its weird because this is exactly what I had originally tried. I went back to the form I was working on and it is still not working. I tried it on a new form and now it works.Tillion
@Tillion - totally understand. I often do the same myself. Only to ask a colleague to help me see the forest for the tree's. Cheers.Curler
you solution is not firing the click event, I have some code in click need to execute So I have to call the click method inside the mouse down event inside the right button conditionRansom
@mehdei, you didn't downvote did you? On a winform app, drop a button on the form, press F4 to bring up the properties, select the lightning ⚡ button and map the MouseDown event.Curler
E
4

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

It will work.

Elledge answered 11/11, 2018 at 10:41 Comment(0)
D
-1

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.

Dagan answered 6/10, 2018 at 15:23 Comment(1)
Sorry, but the middle mouse / right mouse do not hit a break point even on the event declaration (or anywhere inside). I recreated a new Solution + project, c# winforms and same situation.Tillion

© 2022 - 2024 — McMap. All rights reserved.