Nitin Kabra
Nitin Kabra

Reputation: 3246

Command line style caret for Textbox in C#.Net

How can I change the textbox blinking caret as command line style caret ie. horizontal cursor in textbox.

Upvotes: 0

Views: 970

Answers (3)

Chris Dunaway
Chris Dunaway

Reputation: 11216

I got it to work using code like the following (thanks to Prabhakantha). I had to use a timer to set the cursor after handling the textbox Enter event:

    private void textBox1_Enter(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Enabled = false;
        CreateCaret(textBox1.Handle, IntPtr.Zero, 6, textBox1.Height);
        ShowCaret(textBox1.Handle);
    }

This seems a little hackish, though. There must be a better way.

Upvotes: 0

Prabhakantha
Prabhakantha

Reputation: 660

Please try the below code

[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);

public Form1()
{
    InitializeComponent();
}

private void Form1_Shown(object sender, EventArgs e)
{
    CreateCaret(textBox1.Handle, IntPtr.Zero, 10, textBox1.Height);
    ShowCaret(textBox1.Handle);
}

Upvotes: 2

Sumudu Kurukulasuriya
Sumudu Kurukulasuriya

Reputation: 31

Please try below sample code.

this.Cursors = Cursor.None;
this.cursors = cursor.pointer or cursor.arrow

Upvotes: 1

Related Questions