Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the TextBox highlight color when a user selects text?

Tags:

c#

winforms

I've been looking for the way to change the textbox highlight color when a user select text. Windows uses blue as default color. For example, on Microsoft Outlook, when you write a mail and select (highlight) text, the back color is gray.

Selected text in Outlook

TextBox selected text by user

Everybody said that I need to override onPaint method but i don't know how exactly to do that. The RichTextbox selectedbackground color is not the solution because it changes the color for the text, not when the user selects it.

like image 855
Tanner Ornelas Avatar asked Jan 07 '16 22:01

Tanner Ornelas


People also ask

How do I change the color of my highlighted text?

Highlight selected text Select the text that you want to highlight. Go to Home and select the arrow next to Text Highlight Color. Select the color that you want. Note: Use a light highlight color if you plan to print the document by using a monochrome palette or printer.

How do you highlight a textbox in HTML?

Highlight using the HTML5 <mark> tag If you are working on an HTML5 page, the <mark> tag can quickly highlight text. Below is an example of the how to use the mark tag and its result. If your browser supports the <mark> tag, "highlighted text" should have a yellow background.


2 Answers

As an option, you can rely on an ElementHost Windows Forms control to host a WPF TextBox control. Then for the WPF TextBox control, set SelectionBrush and SelectionOpacity.

Example

In the following example I've created a Windows Forms UserControl containing an ElementHost to host a WPF TextBox control. Then for the WPF TextBox control, set SelectionBrush and SelectionOpacity.

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Media;
public class MyWPFTextBox : System.Windows.Forms.UserControl
{
    private ElementHost elementHost = new ElementHost();
    private TextBox textBox = new TextBox();
    public MyWPFTextBox()
    {
        textBox.SelectionBrush = new SolidColorBrush(Colors.Gray);
        textBox.SelectionOpacity = 0.5;
        textBox.TextAlignment = TextAlignment.Left;
        textBox.VerticalContentAlignment = VerticalAlignment.Center;
        elementHost.Dock = System.Windows.Forms.DockStyle.Fill;
        elementHost.Name = "elementHost";
        elementHost.Child = textBox;
        textBox.TextChanged += (s, e) => OnTextChanged(EventArgs.Empty);
        Controls.Add(elementHost);
    }
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override string Text
    {
        get { return textBox.Text; }
        set { textBox.Text = value; }
    }
}

Referenced Assemblies

Here are required referenced assemblies: PresentationCore, PresentationFramework, WindowsBase, WindowsFormsIntegration.

like image 66
Reza Aghaei Avatar answered Sep 28 '22 04:09

Reza Aghaei


Hi here is the code to change the selection colour just keep in mind that you will have to store the current colour and then once you've changed the colour and your application closes you would need to restore it because this changes the colour of the whole computer not just for the current process.

    [DllImport("user32.dll")]
    static extern bool SetSysColors(int cElements, int[] lpaElements, uint[] lpaRgbValues);


    void ChangeSelectColour(Color color)
    {
        const int COLOR_HIGHLIGHT = 13;
        const int COLOR_HIGHLIGHTTEXT = 14;
        // You will have to set the HighlightText colour if you want to change that as well.


        //array of elements to change
        int[] elements = { COLOR_HIGHLIGHT };


        List<uint> colours = new List<uint>();
        colours.Add((uint)ColorTranslator.ToWin32(color));

        //set the desktop color using p/invoke
        SetSysColors(elements.Length, elements, colours.ToArray());
    }
like image 21
Johan J v Rensburg Avatar answered Sep 28 '22 03:09

Johan J v Rensburg