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.
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;
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With