How to hide the caret in a RichTextBox?
Asked Answered
R

9

11

Just like the title: I've searched the web for an answer, but i was not able to find a way to hide the caret of a RichTextBox in VB.NET.

I've tried to set the RichTextBox.Enabled property to False and then change the background color and foreground color to non-grey ones but that did not do the trick.

Thanks in advance.

Regiment answered 24/2, 2009 at 15:56 Comment(4)
I know this is an old post, and is labeled .NET 3.5. However I'd like to give an update to this for those who searched it and can use .NET 4.0. In .NET 4.0, the RichTextBox control now has a property IsReadOnlyCaretEnabled that defaults to False, so the caret is automatically hidden if you set ReadOnly to True. If you want a ReadOnly RichTextBox to show the caret, you can set IsReadOnlyCaretEnabled to True.Plebe
My answer solves you problem? If so, you may consider marking it as an answer. It is in C#, but should work in VB, I think. :)Astrometry
Hi, can you consider marking my answer as solution?Astrometry
In fact I think the easiest way is to use another transparent control to cover it! It can avoid the introduction of non-managed code.Lenlena
A
17

Solution:

from: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21896403.html

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

public class ReadOnlyRichTextBox : System.Windows.Forms.RichTextBox
{

  [DllImport("user32.dll")]
  private static extern int HideCaret (IntPtr hwnd);

  public ReadOnlyRichTextBox()
  {
    this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ReadOnlyRichTextBox_Mouse);
    this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ReadOnlyRichTextBox_Mouse);
    base.ReadOnly = true;
    base.TabStop = false;
    HideCaret(this.Handle);
  }


  protected override void OnGotFocus(EventArgs e)
  {
    HideCaret(this.Handle);
  }

  protected override void OnEnter(EventArgs e)
  {
    HideCaret(this.Handle);
  }

  [DefaultValue(true)]
  public new bool ReadOnly
  {
    get { return true; }
    set { }
  }

  [DefaultValue(false)]
  public new bool TabStop
  {
    get { return false; }
    set { }
  }

  private void ReadOnlyRichTextBox_Mouse(object sender, System.Windows.Forms.MouseEventArgs e)
  {
    HideCaret(this.Handle);
  }

  private void InitializeComponent()
  {
    //
    // ReadOnlyRichTextBox
    //
    this.Resize += new System.EventHandler(this.ReadOnlyRichTextBox_Resize);

  }

  private void ReadOnlyRichTextBox_Resize(object sender, System.EventArgs e)
  {
    HideCaret(this.Handle);

  }
}
Astrometry answered 9/9, 2011 at 13:7 Comment(0)
O
5

Place the richTextBox control on the form

Set form name as Form1

Set richTextBox name as richTextBox1

If you do not want to allow user to copy the text set richTextBox1 ShortcutsEnabled property to False

Go to Project->Add Component, enter name of the component ReadOnlyRichTextBox.cs

Then open ReadOnlyRichTextBox.cs and paste following code:

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

namespace <Replace with your app namespace>
{
    public partial class ReadOnlyRichTextBox : RichTextBox
    {
        [DllImport("user32.dll")]
        static extern bool HideCaret(IntPtr hWnd);

        public ReadOnlyRichTextBox()
        {
            this.ReadOnly = true;
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            HideCaret(this.Handle);
        }
    }
}

From Solution Explorer open your "Form1.Designer.cs" and replace in this file the following lines:

this.richTextBox1 = new System.Windows.Forms.RichTextBox();

with

this.richTextBox1 = new ReadOnlyRichTextBox();

and

private System.Windows.Forms.RichTextBox richTextBox1;

with

private ReadOnlyRichTextBox richTextBox1;

Occupant answered 2/11, 2017 at 11:59 Comment(0)
F
3
/// <summary>
/// Transparent RichTextBox
/// To change BackColor add a Panel control as holder of RichTextLabel
/// </summary>
public class RichTextLabel : RichTextBox
{
    public RichTextLabel()
    {
        base.Enabled = false;
        base.ReadOnly = true;
        base.ScrollBars = RichTextBoxScrollBars.None;
        base.ForeColor = Color.FromArgb(0, 0, 1);
    }

    override protected CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20;
            return cp;
        }
    }

    override protected void OnPaintBackground(PaintEventArgs e)
    {
    }
}
Fraternize answered 23/2, 2011 at 21:1 Comment(0)
A
2

You can use the HideCaret API function, Check it out on www.pinvoke.net. The trick is to know when to call it. A very simple and dirty solution is to start a one-shot timer in the RTF's Enter event. Trapping the correct message in the WndProc as nobugs suggestes is better, unfortunately the message trapped is wrong...

Aslam answered 24/2, 2009 at 17:52 Comment(0)
W
2

This works for me :

public class RichTextLabel : RichTextBox
{
    public RichTextLabel()
    {
        base.ReadOnly = true;
        base.BorderStyle = BorderStyle.None;
        base.TabStop = false;
        base.SetStyle(ControlStyles.Selectable, false);
        base.SetStyle(ControlStyles.UserMouse, true);
        base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

        base.MouseEnter += delegate(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        };
    }

    protected override void WndProc(ref Message m) {
        if (m.Msg == 0x204) return; // WM_RBUTTONDOWN
        if (m.Msg == 0x205) return; // WM_RBUTTONUP
        base.WndProc(ref m);
    }
}

I hope it helps

Witchcraft answered 8/7, 2009 at 0:25 Comment(1)
There are cursor flickering efect on mouse move with this solutionFraternize
S
2

I know it's old, but i see a lot of different options here. I must add that for me, this did the trick :

this.textRichBox.ReadOnly = false;
this.textRichBox.TabStop = false;
Secretory answered 5/7, 2018 at 6:41 Comment(1)
This works until you click on the rich text box. Then the caret comes back. :(Doig
I
1

Here I have a Rich Text control named txtMessage, it's events are handled to hide caret on events that would show it.

<System.Runtime.InteropServices.DllImport("user32.dll")>
Private Shared Function HideCaret(ByVal hWnd As IntPtr) As Boolean
End Function

Public Sub New()
    txtMessage.ReadOnly = True
    txtMessage.TabStop = False
End Sub

Private Sub txtMessage_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMessage.KeyUp
    HideCaret(txtMessage.Handle)
End Sub

Private Sub txtMessage_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtMessage.MouseDown
    HideCaret(txtMessage.Handle)
End Sub

Private Sub txtMessage_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtMessage.SelectionChanged
    HideCaret(txtMessage.Handle)
End Sub
Include answered 14/1, 2011 at 9:42 Comment(0)
T
0

Do something to keep it from getting the 'input focus': it will have the caret, and be editable, only while it has the focus.

Tuberculate answered 24/2, 2009 at 15:56 Comment(0)
S
0

Minimal Version (optimized):

public class ReadOnlyRichTextBox : RichTextBox
{
    [DllImport("user32.dll")]
    static extern bool HideCaret(IntPtr hWnd);

    public ReadOnlyRichTextBox()
    {
        ReadOnly = true;
        SetStyle(ControlStyles.Selectable, false);
    }

    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        HideCaret(this.Handle);
    }

    protected override void OnEnter(EventArgs e)
    {
        base.OnEnter(e);
        HideCaret(this.Handle);
    }

    protected override void OnGotFocus(EventArgs e)
    {
        base.OnGotFocus(e);
        HideCaret(this.Handle);
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
        if (e.Button == MouseButtons.Left)
            HideCaret(this.Handle);
    }
}
Silsbye answered 11/11, 2015 at 18:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.