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;
    }
}
Kairan
  • 5,342
  • 27
  • 65
  • 104
  • 2
    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`.. – TaW Oct 06 '18 at 13:54
  • @TaW 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. – Kairan Oct 06 '18 at 14:03
  • I know you tried as you wrote it. But it still works here. Where do you put the breakpoint? The `switch` statement? – TaW Oct 06 '18 at 14:21
  • @TaW Break point is on the switch line – Kairan Oct 06 '18 at 14:53
  • 1
    This is [by design](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/TextBoxBase.cs,8c9d4490b578c0ee) 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 :) – Hans Passant Nov 11 '18 at 11:45
  • I know this may seem silly but have you tried : `this.textBox1.MouseDown += ...` – roozbeh S Nov 12 '18 at 07:52

3 Answers3

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;
    }
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 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. – Kairan Nov 12 '18 at 22:10
  • @Kairan - totally understand. I often do the same myself. Only to ask a colleague to help me see the forest for the tree's. Cheers. – Jeremy Thompson Nov 13 '18 at 03:50
  • 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 condition – Mehdei Oct 08 '20 at 11:29
  • 1
    @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. – Jeremy Thompson Oct 10 '20 at 12:19
4

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

It will work.

Sasan
  • 149
  • 15
-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.

  • 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. – Kairan Oct 07 '18 at 01:33