AngryC
asked on
How can I hide the caret of a RichTextBox control?
Hello,
Can you please tell me how can I hide the caret of a RichTextBox control completely while my app is running?
I tried this but it didn't work:
using System.Runtime.InteropServ ices;
[DllImport("user32.dll")]
public static extern int ShowCaret(IntPtr hwnd);
HideCaret(richTextBox1.Han dle);
But it didn't work. Please help. Thanks.
Can you please tell me how can I hide the caret of a RichTextBox control completely while my app is running?
I tried this but it didn't work:
using System.Runtime.InteropServ
[DllImport("user32.dll")]
public static extern int ShowCaret(IntPtr hwnd);
HideCaret(richTextBox1.Han
But it didn't work. Please help. Thanks.
Do you want to hide the caret and still be able to edit, or do you just want to prevent the RichTextBox control from getting focus?
Bob
Bob
ASKER
Hello Bob,
It's a read-only RichTextBox BUT the user should be able to select text and right-click on it to show it's contextMenu.
It's a read-only RichTextBox BUT the user should be able to select text and right-click on it to show it's contextMenu.
try to subclass the richeditctrl and do hidecaret whenever you receive paint event
or something like this
protected override void WndProc(ref Message m)
{
base.WndProc (ref m);
HideCaret( this.Handle.ToInt32() );
}
or something like this
protected override void WndProc(ref Message m)
{
base.WndProc (ref m);
HideCaret( this.Handle.ToInt32() );
}
ASKER
Hello Agarici,
Your code worked after I add the following to my exsiting class:
[DllImport("user32.dll", EntryPoint="HideCaret")]
public static extern long HideCaret(IntPtr hwnd);
protected override void WndProc(ref Message m)
{
base.WndProc (ref m);
HideCaret( RichTextBox.Handle );
}
But there's a problem. When the user clicks on the RichTextBox control the caret appears then disappear. And if I hold the mouse button down I see the caret flashing as long as I'm holding the mouse button down :(
How can I solve that?
Also, I'm getting the following error message when I'm closing the form:
ObjectDisposeException was unhandled
Cannot access a disposed object.
Object name: 'RichTextBox'.
Thanks a lot.
Your code worked after I add the following to my exsiting class:
[DllImport("user32.dll", EntryPoint="HideCaret")]
public static extern long HideCaret(IntPtr hwnd);
protected override void WndProc(ref Message m)
{
base.WndProc (ref m);
HideCaret( RichTextBox.Handle );
}
But there's a problem. When the user clicks on the RichTextBox control the caret appears then disappear. And if I hold the mouse button down I see the caret flashing as long as I'm holding the mouse button down :(
How can I solve that?
Also, I'm getting the following error message when I'm closing the form:
ObjectDisposeException was unhandled
Cannot access a disposed object.
Object name: 'RichTextBox'.
Thanks a lot.
Try this custom RichTextBox class:
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServ ices;
public class ReadOnlyRichTextBox : System.Windows.Forms.RichT extBox
{
[DllImport("user32.dll")]
private static extern int HideCaret (IntPtr hwnd);
ReadOnlyRichTextBox()
{
this.MouseDown += new System.Windows.Forms.Mouse EventHandl er(this.Re adOnlyRich TextBox_Mo use);
this.MouseUp += new System.Windows.Forms.Mouse EventHandl er(this.Re adOnlyRich TextBox_Mo use);
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.Mouse EventArgs e)
{
HideCaret(this.Handle);
}
}
Bob
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServ
public class ReadOnlyRichTextBox : System.Windows.Forms.RichT
{
[DllImport("user32.dll")]
private static extern int HideCaret (IntPtr hwnd);
ReadOnlyRichTextBox()
{
this.MouseDown += new System.Windows.Forms.Mouse
this.MouseUp += new System.Windows.Forms.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(
{
HideCaret(this.Handle);
}
}
Bob
ASKER
After creating a new class and pasting your code and then...
private void Form1_Load(object sender, EventArgs e)
{
ReadOnlyRichTextBox cxz = new ReadOnlyRichTextBox();
//this.Controls.Add(cxz);
}
I got:
Error 1 'ReadOnlyRichTextBox.ReadO nlyRichTex tBox()' is inaccessible due to its protection level
What's wrong?
private void Form1_Load(object sender, EventArgs e)
{
ReadOnlyRichTextBox cxz = new ReadOnlyRichTextBox();
//this.Controls.Add(cxz);
}
I got:
Error 1 'ReadOnlyRichTextBox.ReadO
What's wrong?
ASKER
Ok I added 'public' before ReadOnlyRichTextBox() constructor and it worked very good.
But the cursor is appearing when the form is resized.
But the cursor is appearing when the form is resized.
i think the problem is you have added the winproc to the form class
i was suggesting you derive a new class from richtextbox and add the winproc to it.
also this is the reason you get the exception.
try something like:
public class ReadOnlyRichTextBox : System.Windows.Forms.RichT extBox
{
[DllImport("user32.dll")]
private static extern int HideCaret (IntPtr hwnd);
ReadOnlyRichTextBox()
{
}
protected override void WndProc(ref Message m)
{
base.WndProc (ref m);
HideCaret( this.Handle.ToInt32() );
}
}
in my test program this seems to be working as expected
hth,
A.
i was suggesting you derive a new class from richtextbox and add the winproc to it.
also this is the reason you get the exception.
try something like:
public class ReadOnlyRichTextBox : System.Windows.Forms.RichT
{
[DllImport("user32.dll")]
private static extern int HideCaret (IntPtr hwnd);
ReadOnlyRichTextBox()
{
}
protected override void WndProc(ref Message m)
{
base.WndProc (ref m);
HideCaret( this.Handle.ToInt32() );
}
}
in my test program this seems to be working as expected
hth,
A.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
https://www.experts-exchange.com/questions/20597134/Hiding-the-Caret-in-a-RichTextBox-Control.html#8412211
Because it doesn't work.
I am using VS 2005.