Prevent Blinking Cursor in Textbox

2020-02-05 05:28发布

问题:

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."

回答1:

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


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


回答3:

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


回答4:

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


回答5:

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


回答6:

Set the ReadOnly property on the TextBox to true.

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