Disable selecting text in a TextBox

37,967

Solution 1

Attach to the SelectionChanged event, and inside the event set e.Handled = true; and the SelectionLength = 0; and that will stop the selection from occuring. This is similar to what it takes to keep a key press from happening.

Solution 2

If you put the text into a label and then put the label into a System.Widnows.Forms.Panel control that has AutoScroll turned on you can display the text w/o it being selectable.

Solution 3

To disable selection highlight in a TextBox, you can override WndProc and handle WM_SETFOCUS message and replace it with a WM_KILLFOCUS. Please be aware that it doesn't make the TextBox control read-only and if you need to make it read-only, you should also set ReadOnly property to true. If you set ReadOnly to true, you can set and its BackColor to White or any other suitable color which you want.

In below code, I added a SelectionHighlightEnabled property to MyTextBox to make enabling or disabling the selection highlight easy:

  • SelectionHighlightEnabled: Gets or sets a value indicating selection highlight is enabled or not. The value is true by default to act like a normal TextBox. If you set it to false then the selection highlight will not be rendered.
using System.ComponentModel;
using System.Windows.Forms;
public class MyTextBox : TextBox
{
    public MyTextBox()
    {
        SelectionHighlightEnabled = true;
    }
    const int WM_SETFOCUS = 0x0007;
    const int WM_KILLFOCUS = 0x0008;
    [DefaultValue(true)]
    public bool SelectionHighlightEnabled { get; set; }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_SETFOCUS && !SelectionHighlightEnabled)
            m.Msg = WM_KILLFOCUS;

        base.WndProc(ref m);
    }
}

Solution 4

I came across of this thread for my same issue I faced. Somehow I resolved it as below,

if (sender != null)
                {
                    e.Handled = true;
                    if((sender as TextBox).SelectionLength != 0)
                        (sender as TextBox).SelectionLength = 0;
                }

Verifying if the length changed other than 0, then only set it to 0, resolves the recursive loop.

Solution 5

Since the standard TextBox doesn't have the SelectionChanged event, here's what I came up with.

private void TextBox1_MouseMove(object sender, MouseEventArgs e)
{
    TextBox1.SelectionLength = 0;
}
Share:
37,967
Victor
Author by

Victor

Thrilled to learn new things. Love programming, coffee and mountain bikes.

Updated on July 18, 2022

Comments

  • Victor
    Victor almost 2 years

    I have a textbox with the following (important) properties:

    this.license.Multiline = true;
    this.license.ReadOnly = true;
    this.license.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
    this.license.ShortcutsEnabled = false;
    

    It looks like this:

    Textbox with highlighted text in it

    How can I disable the user to highlight text in this textbox (I do not want to disable the textbox completely)?

  • checho
    checho over 10 years
    I couldn't find such event for the WinForms TextBox control.
  • Baz Guvenkaya
    Baz Guvenkaya over 9 years
    This is not a right solution. You can still select the text with a couple of clicking effort.
  • Calcolat
    Calcolat over 8 years
    @checho In case you're interested, I ran into the same issue as you but resolved it by using the WinForms RichTextBox control, which thankfully does have the SelectionChanged event.
  • tdemay
    tdemay almost 8 years
    A MUCH MUCH better solution. No hacks to make a text box work.
  • JG in SD
    JG in SD almost 6 years
  • floydheld
    floydheld over 5 years
    Doing this with a TextBox results in an recursive loop and StackOverflowException. Changing the selection inside the SelectionChanged callback is a bad idea!
  • Lukáš Koten
    Lukáš Koten over 3 years
    @floydheld You can unregister the hander right before the SelectionAll and again register it right after to prevent the StackOverflowException.
  • Death GOD 7
    Death GOD 7 over 2 years
    Also for a better solution, you can add events to clamchoda answer like: MouseLeave and KeyPress MouseLeave for deselecting when the mouse is no longer in the textbox area and KeyPress is for deselecting when the user press the Shift + Key Up/Down for selection.
  • Death GOD 7
    Death GOD 7 over 2 years
    To fix the additional "clicking problem" that Baz said, just see my comment in this post.