Prevent blinking cursor in text box

In a text box, how can I prevent the blinking cursor from appearing when I click on it?

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

This text box is part of the form window that is created to model lan messenger. I am using C #. The form has two text fields to resemble google words. It would be desirable to prevent the blinking cursor from appearing in the upper text box.

I tried:

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

I get an error: "DllImport not found."

+6
c # winforms textbox
source share
6 answers

If you want to prevent editing in the text box, set the ReadOnly property to true.

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

 [System.Runtime.InteropServices.DllImport("user32.dll")] static extern bool HideCaret(IntPtr hWnd); ... HideCaret(myTextBox.Handle); 
+6
source share

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); } } 
+3
source share

if you put the hideCaret function inside the Textchange event, this will solve the problem.

  [DllImport("user32.dll")] static extern bool HideCaret(IntPtr hWnd); private void textBox1_TextChanged(object sender, EventArgs e) { HideCaret(textBox1.Handle); } 
+2
source share

I tried to figure out how to emulate the web address of the Chrome web address (partially) on the TextBox and compiled the code from both here and one .

It works fine, first Clic, selects all the text without showing flashing carriages, the trick is to show the carriage when you click Clic on the selected text again, as it behaves in the address bar of the Chrome browser. Here is 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(); }); } 
+1
source share

Set the ReadOnly property on the TextBox to true .

Other answers to this question: C # read-only text box

0
source share

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 
0
source share

All Articles