johnc
johnc

Reputation: 40233

C# Winforms Suppress a Mouse Click event on a Text Box

I have a multi-line text box on a Winforms app that I want to be able to click on in some states of the app without changing the Text selection.

I still want to subscribe to the MouseDown (or Click) event, I just don't want the text selection to react to it.

I was hoping for a Handled or Cancel property on the MouseEventArgs, but sadly, no.

I could listen to the selection changed event and try to reset it when required, but this feels a little hack-like as I would have to remember current selection before the selection change event. Does anyone have any better suggestions than that?

Upvotes: 0

Views: 7951

Answers (3)

Darien Ford
Darien Ford

Reputation: 1049

You could create your own Textbox inheriting from System.Windows.Forms.Textbox and then override the WndProc.

The WndProc is the initial method which receives messages about everything from the operating system, power messages, input, etc. The name of the method is a throwback to the Win32 (and Win16) days where one of the two main functions you would implement was the "WndProc" or Windows Procedure.

By doing so, you can now intercept the windows messages for the mouse (or any other message) prior to the events being dispatched to the base implementation.

In the below example, we do not pass on any Left button down, up or double click to the base control to process.

public partial class MyTextBox : TextBox
{
    int WM_LBUTTONDOWN = 0x0201; //513
    int WM_LBUTTONUP = 0x0202; //514
    int WM_LBUTTONDBLCLK = 0x0203; //515

    public MyTextBox()
    {
        InitializeComponent();
    }

    protected override void WndProc(ref Message m)
    {

        if (m.Msg == WM_LBUTTONDOWN || 
           m.Msg == WM_LBUTTONUP || 
           m.Msg == WM_LBUTTONDBLCLK // && Your State To Check
           )
        {
            //Dont dispatch message
            return;
        }

        //Dispatch as usual
        base.WndProc(ref m);
    }
}

Only thing left to do is adding checking for your state to determine when to pass on the messages or not.

You can find a list of the Windows messages to process here.

Upvotes: 9

marcc
marcc

Reputation: 12409

How about handling the Enter event and setting the focus to a different control in this event? Still pretty "hack-like" though, just with less code.

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 416149

Layer a transparent panel over top of it.

Upvotes: 0

Related Questions