How can I hide the caret of a RichTextBox control?  
Author Message
Angry Coder





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

Hello,

Can you please tell me how can I hide the caret of a RichTextBox control completely while my app is running

Thanks.


Windows Forms24  
 
 
Karthikeya Pavan Kumar .B





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

Just pass the handle for your control and the API will hide the caret inside the control ...

You can use dllimport to use this api and then pass the appropriate handle to get the desired effect.

[DllImport("user32.dll", EntryPoint="ShowCaret")]
public static extern long ShowCaret(long hwnd);

[DllImport("user32.dll", EntryPoint="HideCaret")]
public static extern long HideCaret(long hwnd);

Now call these methods and then pass the appropriate handle of the control to make the caret dissapear. You must first set focus to the object for which you want to create caret then create caret. when you lost focus, it is automatically deleted/destroyed.

Hope this helps !

 
 
Angry Coder





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

Thanks a lot but I had to change: HideCaret(long hwnd); to HideCaret(intPtr hwnd); and it didn't work. :(

The program is compiling fine but the caret is not disappearing!

I didn't understand your last paragraph! Please make it simple. I'm new to C# :)



 
 
Karthikeya Pavan Kumar .B





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

Check this link probably it will help you.

http://blogs.technet.com/david_bennett/archive/2005/04/06/403402.aspx



 
 
Angry Coder





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

It didn't. The caret is not disappearing :(

 
 
James Curran





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

What actually is your goal here. Hiding the caret on a textbox where you expect someone to type is just plain cruel, so I'm guessing you don't want the user to type, inwhich case, you can probably accomplish what you want setting Enabled = false or ReadOnly = true;

 
 
Angry Coder





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

Hello James,

My RichTextBox is read-only but I can't set the enabled property to false because I want the user to be able to select text to copy it, exactly like a webbrwoser.

 
 
James Curran





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

WebBrowsers have a caret to show you what text you are selecting.

 
 
Angry Coder





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

I didn't mean a textbox on a webbrwoser, or any form element.

 
 
James Curran





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

>> I didn't mean a textbox on a webbrwoser, or any form element.

Neither did I. Run your mouse over this very line of text. It will change to a caret.



 
 
Angry Coder





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

OK :)
I want my richTextBox to be exactly like that.

 
 
James Curran





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

>> I want my richTextBox to be exactly like that.

And how is your read-only RichTextBox deviating from that



 
 
Bobbias





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

he's refering to how if you click a word in the RichTextBox, you see the caret that flashes, as well as the cursor caret icon. He just wants you to see the curser caret icon there, and wants it to act in the way that a webpage works where you can select text, but have no blinking caret on the page (unless you're in a text box of some sort).
 
 
cablehead





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

This will completely hide it:

Set the timer1.Interval = 1;

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

 

namespace WindowsApplication2

{

    public partial class Form1 : Form

    {

        private bool hidden;

        private const UInt32 WM_LBUTTONDOWN = 0x201;

        private const UInt32 WM_LBUTTONUP = 0x202;

 

        [DllImport("user32.dll")]

        private static extern int SendMessage(IntPtr handle, UInt32 message,

        int wParam, int lParam);

 

        [DllImport("user32.dll", EntryPoint = "ShowCaret")]

        public static extern long ShowCaret(IntPtr hwnd);

 

        [DllImport("user32.dll", EntryPoint = "HideCaret")]

        public static extern long HideCaret(IntPtr hwnd);

 

        public Form1()

        {

            InitializeComponent();

        }

 

        private void timer1_Tick(object sender, EventArgs e)

        {

            SendMessage(richTextBox1.Handle, WM_LBUTTONDOWN, 0, 0);

            SendMessage(richTextBox1.Handle, WM_LBUTTONUP, 0, 0);

            timer1.Stop();

        }

 

        private void richTextBox1_Click(object sender, EventArgs e)

        {

            if (!hidden)

            {

                hidden = true;

                HideCaret(richTextBox1.Handle);

            }

        }

 

       

    }

}

 



 
 
Elmer_927





PostPosted: Windows Forms General, How can I hide the caret of a RichTextBox control? Top

can u convert it to vb.net Thanks!