Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide the caret in a RichTextBox?

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.

like image 910
pistacchio Avatar asked Feb 24 '09 15:02

pistacchio


2 Answers

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;

like image 150
Ramil Shavaleev Avatar answered Oct 09 '22 17:10

Ramil Shavaleev


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);

  }
}
like image 15
Pedro77 Avatar answered Oct 09 '22 17:10

Pedro77