6

In a textbox, how can u prevent the display of the blinking cursor when you click on it?

I did read in some forums that there is call to a particular api but when i tried it in my code, an error was shown. Please provide the complete code for this purpose if possible and let me know if there is a particular event where the code should be executed.

This textbox is part of a form window that am creating for the simulation of a lan messenger. I am using C#. The form has two textboxes in order to resemble that of google talk. It would be desirable to prevent displaying the blinking cursor on the upper textbox.

I tried:

[DllImport("user32")] 
private static extern bool HideCaret(IntPtr hWnd); 
public void HideCaret() { HideCaret(TextBox1.Handle); } 

I get the error: "DllImport could not be found."

Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202
Avik
  • 2,097
  • 7
  • 24
  • 30
  • Please provide more information about this textbox and what language it was created in. A native cocoa textbox on OS X? An textbox in a form on an html web page? A textbox in a win32 app? A gtk2 textbox? A BeOS textbox? etc. – Nathan Stocks Mar 02 '09 at 04:20
  • Well this textbox is part of a form window that am creating for the simulation of a lan messenger.I am using c#. The form has two textboxes in order to resemble that of google talk and it would be desirable to prevent displaying the blinking cursor on the upper textbox. – Avik Mar 02 '09 at 04:26
  • Edit the question with your new information - people aren't going to see it as well if it's in the comments. – David Z Mar 02 '09 at 04:27
  • Which "particular api" did you try? When you tried it, which "error was shown"? Stack Overflow readers can't see what you're doing unless you tell them. – Greg Hewgill Mar 02 '09 at 04:31
  • [DllImport("user32")] private static extern bool HideCaret(IntPtr hWnd); public void HideCaret() { HideCaret(TextBox1.Handle); } This is what the code I had tried implementing. But it shows that DllImport could not be found. – Avik Mar 02 '09 at 04:38
  • How are you going to know where the cursor is if you hide it? – cdonner Mar 02 '09 at 04:42
  • Like in the chat window of google talk where the blinking cursor is hidden it the upper textbox, in my form window too i have 2 textboxes where character typed in the lower textbox shall be transferred to the upper textbox when i click submit. – Avik Mar 02 '09 at 04:49
  • 4
    I think he just forgot to write `using System.Runtime.InteropServices;` or alternatively use full spec when using DllImport: `[System.Runtime.InteropServices.DllImport("user32")] ...` – Sinatr Mar 04 '13 at 11:06
  • Duplicate of [How to disable cursor in textbox?](https://stackoverflow.com/questions/3730968/how-to-disable-cursor-in-textbox) – TylerH Dec 21 '19 at 06:17

8 Answers8

7

If you want to disallow editing on the textbox, set it's ReadOnly property to true.

If you want to allow editing but still hide the caret, call the Win32 API exactly as specified:

[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);

...

HideCaret(myTextBox.Handle);
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
  • This is the code that I had come across before too.The problem here is that an error is being displayed saying that:"Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)" Is there any specific reference to made?? – Avik Mar 02 '09 at 04:59
  • Add using statement at the top. using System.Runtime.InteropServices; – shahkalpesh Mar 02 '09 at 05:01
  • Sadly, I have to agree with helrich. The blinking caret still stays after clicking the textbox, even if it wasn't set to read-only. – Kaitlyn Aug 06 '15 at 18:09
2

Hi, Try this code

public class CustomTextBox:System.Windows.Forms.TextBox
{
    [System.Runtime.InteropServices.DllImport("user32")]
    private static extern bool HideCaret(IntPtr hWnd);

    public CustomTextBox()
    {
        TabStop = false;

        MouseDown += new System.Windows.Forms.MouseEventHandler(CustomTextBox_MouseDown);
    }

    void CustomTextBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        HideCaret(this.Handle);
    }
}
2

Putting the hideCaret function inside the TextChanged event will solve the problem:

[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);

private void textBox1_TextChanged(object sender, EventArgs e)
{
    HideCaret(textBox1.Handle);
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
kral06
  • 119
  • 1
  • 1
  • Please do not write the exact same answer on [multiple questions](https://stackoverflow.com/a/15817260/2756409). Instead, flag or vote to close the newer question as a duplicate of the other. – TylerH Dec 21 '19 at 06:17
1

I had a problem that could not hide the Caret in the textBox. The trick is to hide it in GotFocus event which is not accessible in Designer, therefore have to subscribe it manually. Complete code below:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApp
{
    public partial class Form1 : Form
    {
        [DllImport("user32")]
        private static extern bool HideCaret(IntPtr hWnd);

        public Form1()
        {
            InitializeComponent();
            textBox1.GotFocus += textBox1_GotFocus;
        }

        private void textBox1_GotFocus(object sender, EventArgs e)
        {
            HideCaret(textBox1.Handle);
        }
    }
}
Creek Drop
  • 464
  • 3
  • 13
0

I am able to emulate Chrome's web address bar (partially) on a TextBox using code from both here and this answer.

On first click, it selects all the the text without showing the blinking caret, the trick is to make the caret show itself when you click a second time on the selected text, which is how Chrome's web address bar behaves.

Here's the code:

[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);

private void textBox2_Enter(object sender, EventArgs e)
{
    // Kick off SelectAll asyncronously so that it occurs after Click
    BeginInvoke((Action)delegate
    {
        HideCaret(textBox2.Handle); 
        textBox2.SelectAll();              
    });          
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
WhySoSerious
  • 1,930
  • 18
  • 18
  • This does not answer the question that was asked. OP wants to hide the cursor on *load*, not *select all the text* on *click*. – TylerH Dec 21 '19 at 06:14
0

Others have stated that setting "ReadOnly" to "True" will avoid the caret when you click on a textbox. They are mistaken there. I am using VB.Net but maybe my sample code below might provide a simpler answer.

It involves creating a new class (MyTextBox) with the added property of "NoCaret" which defaults to "False" (i.e. it shows caret by default). Add the following code to your project in a new class module, rebuild the project, then drag your new textbox (then found at top of your Toolbox) onto your form. To stop the caret showing tick "NoCaret" in the properties panel for each MyTextbox control you add to your forms.

So, you start by creating a new Class Module as follows (drawing the textbox in the designer is not necessary. It is actually better NOT to draw it since that will create unnecessary "InitializeComponent" code that will just muddy the waters. The line "MyBase.New()" in the sub "New()" takes care of everything)

Imports System.Runtime.InteropServices

Public Class MyTextBox
    Inherits TextBox
    Private Declare Function HideCaret Lib "user32.dll" (ByVal hwnd As Int32) As Int32
    Private Declare Function ShowCaret Lib "user32.dll" (ByVal hwnd As Int32) As Int32

    Private _HideCaret As Boolean

    Public Sub New()
        MyBase.New()
    End Sub

    Public Property NoCaret As Boolean
        Get
            Return _HideCaret
        End Get
        Set(ByVal value As Boolean)
            _HideCaret = value
        End Set
    End Property

    Private Sub TextBox_MouseMove(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.MouseMove
        If Me.NoCaret = True Then
            HideCaret(Handle.ToInt32)
        Else
            ShowCaret(Handle.ToInt32)
        End If
    End Sub
End Class

Make sure, though, that any EXISTING code that is making a "TypeOf" test on any existing textboxes on your form such as:

For each tb as control in MyForm.Controls
   if TypeOf tb Is TextBox then
   .....{do something}
   endif
Next

is changed to:

For each tb as control in MyForm.Controls
   if TypeOf tb.Parent Is TextBox then
   .....{do something}
   endif
Next
Chris Raisin
  • 384
  • 3
  • 7
-1

VB.NET Code

Imports System.Runtime.InteropServices

Public Class xxxxxxxxxxxxxxxxxxxxxx

<DllImport("user32.dll")>
    Private Shared Function HideCaret(ByVal hwnd As IntPtr) As Boolean
    End Function

...............

Private Sub txtNotePreview_MouseMove(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNotePreview.MouseMove, txtNotePreview.KeyPress
        HideCaret(txtNotePreview.Handle)
    End Sub
Olegan
  • 1
-2

Set the ReadOnly property on the TextBox to true.

More answers to this question: Read-only textbox in C#

Community
  • 1
  • 1
Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202