views:

2055

answers:

4

Hi,

Is it possible to make the text in a TextBox unselectable without disabling the TextBox? It's a RichTextBox and I need it's formatting and selection features. I can't disable it because I want to handle MouseMove and MouseDown events.

So far I've thought about disabling the Text box and putting a panel on top of it which will delegate it's events to the textbox handlers, but I can't make the panel transparent so it hides the textbox.

Thanks.

A: 

Why not use a label instead?

Rowland Shaw
It's a RichTextBox and I need it's formatting and selection features.I can't disable it because I want to handle MouseMove and MouseDown events.
Meidan Alon
+1  A: 

What about dealing with the .Enter or .GotFocus events to clear any selection made?
You can see the opposite of what you wanted in Automatically select all text on focus in WinForms TextBox.

Dror
Thanks, I just caught the enter event and set the focus to another control.
Meidan Alon
You do know that that's not advisable! It clearly states in the documentation [MSDN, Control Focus Events] that you should not set focus from within Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. At least, that's how I read it!Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.gotfocus.aspx
Matthew M.
A: 

I'm not quite sure what you're getting at (wanting something unselectable but wanting its selection features), but does setting ReadOnly to true accomplish what you're looking for?

lc
I select specific words programmaticly, but I don't want the user to be able to select anything.
Meidan Alon
Ah. Makes sense now. Go with what nahmiasdror suggested in that case. The user can still affect the selection in ReadOnly mode.
lc
A: 

How about handling selection change event like this:

    private void richTextBox1_SelectionChanged(object sender, EventArgs e)
    {
        this.richTextBox1.SelectionStart = this.richTextBox1.Text.Length;
    }