views:

653

answers:

3

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.

+1  A: 

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...

danbystrom
+1  A: 

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

A: 

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

ChrisW