Solved

How can I hide the caret of a RichTextBox control?

Posted on 2006-06-22
10
3,310 Views
1 Endorsement
Last Modified: 2008-01-09
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.InteropServices;

[DllImport("user32.dll")]
public static extern int ShowCaret(IntPtr hwnd);

HideCaret(richTextBox1.Handle);

But it didn't work. Please help. Thanks.
1
Comment
Question by:AngryC
  • 5
  • 3
  • 2
10 Comments
 

Author Comment

by:AngryC
ID: 16964971
Please don't refer me to this:

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20597134.html#8412211

Because it doesn't work.

I am using VS 2005.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16965697
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
0
 

Author Comment

by:AngryC
ID: 16966406
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.
0
 
LVL 11

Expert Comment

by:Agarici
ID: 16966942
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() );
            }
0
 

Author Comment

by:AngryC
ID: 16970473
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 96

Expert Comment

by:Bob Learned
ID: 16971594
Try this custom RichTextBox class:

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

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

Bob
1
 

Author Comment

by:AngryC
ID: 16972397
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.ReadOnlyRichTextBox()' is inaccessible due to its protection level

What's wrong?
0
 

Author Comment

by:AngryC
ID: 16972922
Ok I added 'public' before ReadOnlyRichTextBox() constructor and it worked very good.

But the cursor is appearing when the form is resized.
0
 
LVL 11

Expert Comment

by:Agarici
ID: 16981598
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.RichTextBox
{

  [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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 30 total points
ID: 16984995
Try this:

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

Bob
1

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now