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
Set the ReadOnly
property on the TextBox
to true
.
More answers to this question: Read-only textbox in C#
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);
}
}
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
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);
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);
}
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