Hi !!! If any one can help me I'll be pleased! I have one TextBox. I entering some text in the Text Property. After that I pressing the "Enter" button and the cursor position it's going of the next line. I would like to go back the carret at the beggining of the same line where it was. Sorry if my English it's Suck.

Dani AI

Generated

Short summary and why the posted snippets didn't work for you

Pressing Enter in a multiline WinForms TextBox inserts a newline as part of the control's default processing. That default processing happens after KeyDown/KeyPress handlers run, so if you set the caret in those handlers the control will often move it again when it inserts the new line. That explains why ’s KeyPress/Select approach can appear to leave the caret on the second line, and why ’s suggestion to make the box single-line won’t help if you need multiline behavior.

Two reliable approaches

  1. Prevent the newline from being inserted, then place the caret where you want. Use KeyDown and call e.SuppressKeyPress to stop the default insertion, then compute the start index of the first line and move the caret there. This keeps the TextBox text unchanged by Enter.

  2. Let the newline happen and then reposition the caret after the insertion. Do that in KeyUp or in TextChanged (but be careful with TextChanged because it fires for every edit). Use the TextBox API to get the first character index of line 0 and scroll to the caret.

Example (safest: suppress Enter in KeyDown and then move caret to the start of line 0)

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        e.SuppressKeyPress = true; // stop the newline being added
        // perform whatever action Enter should trigger...

        int startIdx = textBox1.GetFirstCharIndexFromLine(0);
        if (startIdx >= 0)
        {
            textBox1.SelectionStart = startIdx;
            textBox1.SelectionLength = 0;
            textBox1.ScrollToCaret();
        }
    }
}

If you prefer to allow the newline, move the same reposition code into KeyUp (check e.KeyCode == Keys.Enter) or into TextChanged after detecting the Enter insertion. Also verify AcceptsReturn/Multiline settings match the behavior you want.

Recommended Answers

All 6 Replies

Did you try setting the Multiline property to false.

To move the cursor to the beginning of the textbox you need to do the following:

textBox1.SelectionStart = 0;
textBox1.SelectionLength = 0;
textBox1.Select();

However, where to do this is the problem.
You can not do it in the TextChanged event as the user might still be typing in the text.:confused:

First: I need the TextBox to be Multiline and to be true. Second: I use KeyDown Event not TextChanged. And Thank you for your answer but your example isn't work for me.

textBox1.SelectionStart = 0;
textBox1.SelectionLength = 0;
textBox1.Select();

This example, places the carret at the beginning of the second line. I wish the carret to be returned to at the beggining of the first line.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
    if (e.KeyChar == (char)Keys.Enter) {
        textBox1.Select(0, 0);
    }
}

Thank you for the reply, but this example does not work in my case.
Thanks for your time !!!

Thank you for the reply, but this example does not work in my case.

Works fine when I do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.