4

How can I disable the selection highlight of RichTexBox or TextBox in my Windows Forms Application as shown in the image.

enter image description here

I need to change selection highlight color from Blue to White, because I need to hide selection in TextBox or RichTextBox all the time. I tried to use RichTextBox.HideSelection = true, but it doesn't not work as I expect.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
wong
  • 53
  • 1
  • 5
  • Possible duplicate of [visual studio find and replace highlight color](http://stackoverflow.com/questions/7192536/visual-studio-find-and-replace-highlight-color) – active92 Sep 20 '16 at 08:30
  • thank you for your ans but i need to do in richtextbox – wong Sep 20 '16 at 08:34
  • how about this? http://stackoverflow.com/questions/11183599/rich-text-box-how-to-highlight-text-block – active92 Sep 20 '16 at 08:35
  • You are probably looking for [RichTextBox.SelectionBackColor](https://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionbackcolor%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396) – Lithium Sep 20 '16 at 08:36
  • thank you but RichTextBox.SelectionBackColor can't solve my problem – wong Sep 20 '16 at 08:46
  • i need to change cursor highlight Color not a background font color – wong Sep 20 '16 at 08:48
  • @wong what do you mean by cursor highlight color? – active92 Sep 20 '16 at 09:00
  • @active92 sorry if my english is not good i mean when you click mouse down for select character or string in richtextbox the color is "blue" i need to change color blue to another color – wong Sep 20 '16 at 09:06
  • @wong i get what you mean now but I don't think that is something that you can change in your code. it has to do with you visual studio's settings. – active92 Sep 20 '16 at 09:23
  • set the control's `.SelectionColor = Color.Black` or the color you want; Before that set the `SelectionStart` to 0 and `SelectionLength` to the `Length` of the text in the control – L J Sep 20 '16 at 09:38

1 Answers1

5

You can handle WM_SETFOCUS message of RichTextBox and replace it with WM_KILLFOCUS.

In the following code, I've created a ExRichTextBox class having Selectable property:

  • Selectable: Enables or disables selection highlight. If you set Selectable to false then the selection highlight will be disabled. It's enabled by default.

Remarks: It doesn't make the control read-only and if you need to make it read-only, you should also set ReadOnly property to true and its BackColor to White.

public class ExRichTextBox : RichTextBox
{
    public ExRichTextBox()
    {
        Selectable = true;
    }
    const int WM_SETFOCUS = 0x0007;
    const int WM_KILLFOCUS = 0x0008;

    ///<summary>
    /// Enables or disables selection highlight. 
    /// If you set `Selectable` to `false` then the selection highlight
    /// will be disabled. 
    /// It's enabled by default.
    ///</summary>
    [DefaultValue(true)]
    public bool Selectable { get; set; }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_SETFOCUS && !Selectable)
            m.Msg = WM_KILLFOCUS;

        base.WndProc(ref m);
    }
}

You can do the same for TextBox control.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Works like magic. I would change the code example so `Selectable` will be false... – Michael Haddad Feb 08 '19 at 09:42
  • 1
    @Sipo Good to hear! The reason that I set it to true by default, was because I didn't want to change the default behavior of the `RichtextBox` unless you set the `Selectable` to `false` intentionally. – Reza Aghaei Feb 08 '19 at 09:44
  • Thanks for the quick comment! I think that because the default behavior is exactly what the OP **doesn't** want it would be more suitable... – Michael Haddad Feb 08 '19 at 09:46
  • 1
    @Sipo Thanks for the feedback. I see, I edited the answer a bit, to make it clearer that, to disable selection you need to set the `Selectable` property to `false`. – Reza Aghaei Feb 08 '19 at 09:54