0

In my Windows form application, I have a RichTextBox and a couple of other controls. What I want is after I type some words in the text box then I move the cursor outside of the box, afterwards hit the HOME keystroke. The cursor should return the beginning of the text in the RichTextBox.

I finished this part and works out perfectly.

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Home)
        {
            richTextBox1.SelectionStart = 0;
            Cursor.Position = richTextBox1.PointToScreen(richTextBox1.Location);
        }
    }

However I can't see the cursor, of course no blinking at all. Adding the following code is not working.

Cursor.Show();

My question is how to enable the cursor shows up?

8
  • I think the cursor only shows up when the RichTextBox has focus.
    – eddie_cat
    Jul 16, 2014 at 18:59
  • Maybe this will be helpful: stackoverflow.com/questions/7873563/…
    – eddie_cat
    Jul 16, 2014 at 19:00
  • Cursor is the mouse arrow, you probably mean the text cursor? Jul 16, 2014 at 19:00
  • It's called a Caret, not a Cursor. Just call richTextBox1.Select();
    – LarsTech
    Jul 16, 2014 at 19:01
  • @CommuSoft, yes, maybe. I don't know the precise name.
    – user1108948
    Jul 16, 2014 at 19:01

2 Answers 2

0

Sorry for stupid, by the hint of the comments. The control should be focused. Just add one line code:

richTextBox1.Focus();

Thanks everybody.

0

You can do this:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Home)
    {
        richTextBox1.SelectionStart = 0;
        richTextBox1.Select();
        richTextBox1.Focus();
        Cursor.Position = richTextBox1.PointToScreen(richTextBox1.Location);
    }
}

This will set the Selection start and then select the Rich Text Box. Now if there is no focus even after the Select() method is called, Focus() method will ensure that the control gets the focus.
Hope this works...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.