3

Is there a possibility that after selection made by user, every selected letter displays it's original color? And not always white as it's by default?

I want to achieve something like that enter image description here which you can see in wordpad.

instead of enter image description here which you see in RichTextBox.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Paweł Poręba
  • 1,084
  • 1
  • 14
  • 37
  • I know hot to color different parts, but I want it to be seen while selection. – Paweł Poręba Sep 15 '15 at 17:03
  • `richTextBox1.SelectionColor = System.Drawing.Color.Red;` maybe when you select color you change with this. – Nejc Galof Sep 15 '15 at 19:15
  • No, it doesn't work like this. It will change the color of the selected text or the text from the curren caret position if there is not selection, but it won't affect the color of text WHILE in selection. – Paweł Poręba Sep 15 '15 at 21:00
  • Which event you call this? Maybe you not refreshing while in selection. – Nejc Galof Sep 15 '15 at 21:51
  • I don't think You understand me. I don't need to call an event, cause it's just selection that I am doing programatically. So I just select - that's all. What is more, there is no point at refreshing. Please, read carefully my question. The change of selection text after selection WORKS fine. The color of selection text WHILE in selection is always white, like on the screen I've attached. – Paweł Poręba Sep 16 '15 at 06:58

2 Answers2

11

You can use the latest version of RichTextBox that is RICHEDIT50W, to do so you should inherit from standard RichTextBox and override CreateParams and set the ClassName to RICHEDIT50W:

Code

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class ExRichText : RichTextBox
{
    [DllImport("kernel32.dll", EntryPoint = "LoadLibraryW", 
        CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern IntPtr LoadLibraryW(string s_File);
    public static IntPtr LoadLibrary(string s_File)
    {
        var module = LoadLibraryW(s_File);
        if (module != IntPtr.Zero)
            return module;
        var error = Marshal.GetLastWin32Error();
        throw new Win32Exception(error);
    }
    protected override CreateParams CreateParams
    {
        get
        {
            var cp = base.CreateParams;
            try
            {
                LoadLibrary("MsftEdit.dll"); // Available since XP SP1
                cp.ClassName = "RichEdit50W";
            }
            catch { /* Windows XP without any Service Pack.*/ }
            return cp;
        }
    }
}

Screenshot

enter image description here

Note:

  • I could see the class of RichTextBox of wordpad using Spy++ thanks to this ancient useful visual studio tool.

  • If you had any problem with RICHEDIT50W in your os, you can open Spy++ and WordPad and then select the RichTextBox of it and see what's the class name.

enter image description here

  • When I searched about applying the RICHEDIT50W class to my control, I reached to this great post of @Elmue, Thanks to him.
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • thanks a lot! Now the visibility of a color while selection works perfectly! – Paweł Poręba Sep 17 '15 at 08:17
  • @Reza Aghaei, where do I put this class in my project? because I use richtextbox in more than one form? – Hunar Apr 30 '20 at 11:39
  • When you add the class to your project, if you close all the designers and rebuild the project, the `ExRichText` will appear in toolbox and you can drop it on forms. – Reza Aghaei Apr 30 '20 at 12:03
  • @Reza Aghaei, but when I drag it to my user control, I get this error: https://imgur.com/YAoGPyw , could please tell me how to solve it? – Hunar May 01 '20 at 10:39
2

For anyone having problem with Visual Studio 2019 and exception "Class already exists" as mentioned in Hunar's comment I modified slightly code to load library only once and that solved issue for me.

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;

[DesignerCategory("Code")]
public class RichTextBox5 : RichTextBox
{
    [DllImport("kernel32.dll", EntryPoint = "LoadLibraryW", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern IntPtr LoadLibraryW(string s_File);

    private static readonly object libraryLoadLock = new object();
    private static bool libraryLoadFlag;

    public static IntPtr LoadLibrary(string s_File)
    {
        var module = LoadLibraryW(s_File);
        if (module != IntPtr.Zero)
        {
            return module;
        }
        var error = Marshal.GetLastWin32Error();
        throw new Win32Exception(error);
    }

    protected override CreateParams CreateParams
    {
        get
        {
            var cp = base.CreateParams;
            try
            {
                lock (libraryLoadLock)
                {
                    if (!libraryLoadFlag)
                    {
                        LoadLibrary("MsftEdit.dll"); // Available since XP SP1
                        libraryLoadFlag = true;
                    }
                }
                cp.ClassName = "RichEdit50W";
            }
            catch { /* Windows XP without any Service Pack.*/ }
            return cp;
        }
    }
}

Sorry for answering this way, but due to insufficient reputation points I was able only to post an answer.

  • Any luck with version 7.0 or 8.0 of RichEditBox?https://stackoverflow.com/a/29488625/1908746 –  Feb 26 '21 at 06:43