Prevent Blinking Cursor in Textbox

后端 未结 7 1308
庸人自扰
庸人自扰 2020-12-09 22:33

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

相关标签:
7条回答
  • 2020-12-09 23:14

    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();              
        });          
    }
    
    0 讨论(0)
提交回复
热议问题