1
votes

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

1
Have you ever seen such behavior in an application? Instead of trying to restrict user interaction with your richtextbox, show another richtextbot to user and after he finished his task with second control, add content to the first control.Reza Aghaei
I don't get it @Reza, when the user presses a key and the readonly richtext box is clicked before that (the richtextbox has focus) it produces some error sounds .... at that time .......Walid Mashal
I have never seen such design in an application, so I will avoid following such idea. Instead I will create a second rich text box and let the user do whatever he wants with the second rich text box, after he finished his manipulation and confirmed he finished, then I'll use the content of second control and add it to first control or whatever I want to do.Reza Aghaei
Simply write an event handler for the richtextbox' Enter event. And move the focus back to where you want it with the desired control's Focus() method.Hans Passant
@RezaAghaei richtextbox is used only to display coloured text the user has no interaction with itWalid Mashal

1 Answers

2
votes

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.