Walid Mashal
Walid Mashal

Reputation: 342

make richtextbox not respond to mouse events

I am developing an application and in one of its forms i have placed a richtextbox that has some text that the user will be typing,I have set richtextbox's ReadOnly property to true, form's keypreview to true and I have handled forms keypress event to apply blue color to the correct keypress and red color to the wrong keypress to current character in the richtextbox. now i need to restrict the users to only typing the text, they should not be able to select richtextbox text using mouse caz that way they can mess with my application.

tnx in advance

Upvotes: 1

Views: 557

Answers (1)

Adnan Umer
Adnan Umer

Reputation: 3689

You need to subclass RichTextBox and disable processing of mouse events.

public class DisabledRichTextBox : System.Windows.Forms.RichTextBox
{
    // See: http://wiki.winehq.org/List_Of_Windows_Messages

    private const int WM_SETFOCUS   = 0x07;
    private const int WM_ENABLE     = 0x0A;
    private const int WM_SETCURSOR  = 0x20;

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        if (!(m.Msg == WM_SETFOCUS || m.Msg == WM_ENABLE || m.Msg == WM_SETCURSOR))
            base.WndProc(ref m);
    }
}

It will act like a label, preventing focus, user input, cursor change, without being actually disabled.

You also need to keep ReadOnly = true to disable editing.

Upvotes: 2

Related Questions