How to set TextBox cursor position without SelectionStart

39,619

Solution 1

I have found the solution!

        // save current cursor position and selection 
        int start = textBox.SelectionStart;
        int length = textBox.SelectionLength;

        Point point = new Point();
        User32.GetCaretPos(out point);

        // update text
        textBox.Text = value;

        // restore cursor position and selection
        textBox.Select(start, length);
        User32.SetCaretPos(point.X, point.Y);

Now its working just like it should.

Solution 2

//save position
            bool focused = textBox1.Focused;
            int start = textBox1.SelectionStart;
            int len = textBox1.SelectionLength;
            //do your work
            textBox1.Text = "duviubobioub";
            //restore
            textBox1.SelectionStart = start;
            textBox1.SelectionLength = len ;
            textBox1.Select();

Solution 3

To set the cursor position in textbox without selection start...!

textbox1.Select(textbox1.text.length,0); /* ===> End of the textbox  */
  textbox1.Select(0,0);                    /* ===> Start of the textbox  */
Share:
39,619
walruz
Author by

walruz

I wanted to be a guitarist in a rock'n'roll band. :) Brian Setzer, you know? But now I am a software engeneer (C and C#).

Updated on February 09, 2020

Comments

  • walruz
    walruz about 4 years

    I have a Windows Forms textbox with background thread updating its value every second. If I place cursor inside textbox it will loose its current position at next update. Same thing with text selection.

    I tried to solve it like that

        protected void SetTextProgrammatically(string value)
        {
            // save current cursor position and selection
            int start = textBox.SelectionStart;
            int length = textBox.SelectionLength;
    
            // update text
            textBox.Text = value;
    
            // restore cursor position and selection
            textBox.SelectionStart = start;
            textBox.SelectionLength = length;
        }
    

    It works good most of the time. Here is situation when it does not work:
    1) I place cursor at the end of the text in textbox
    2) press SHIFT and move cursor to the left using <- arrow key
    Selection won't work properly.

    It looks like combination SelectionStart=10 and SelectionLength=1 automatically moves cursor to position 11 (not 10 as I want it to be).

    Please let me know if there is anything I can do about it! I am using Framework.NET 2.0.
    There must be a way to set cursor position in textbox other then SelectionStart+SelectionLength.

  • walruz
    walruz about 11 years
    I don't see the difference between your suggestion and code that I'm already using.
  • Stefano Altieri
    Stefano Altieri about 11 years
    Sorry.. I forgot the Select()
  • walruz
    walruz about 11 years
    Unfortunately, it is not the solution - nothing changed. I still cannot select text inside textbox moving cursor from right to left.
  • Stefano Altieri
    Stefano Altieri about 11 years
    I posted the whole code I used to test the feature and it works for me, including right to left selection
  • walruz
    walruz about 11 years
    It is strange, suggested code shouldn't be working (just as mine). And it still does not work for me. Function Select(start, length) places cursor at the end of selection (you can actually see it). Problem occurs only when you're moving from right to left because in this case you're moving in opposite directions.
  • walruz
    walruz about 11 years
    What I think will solve the problem is something like: textBox1.CaretIndex = start; added to the end of my function.
  • walruz
    walruz about 11 years
    Or some function Select(startIndex, endIndex) which will place cursor to endIndex position (even if endIndex < startIndex)
  • Tanner Ornelas
    Tanner Ornelas about 6 years
    When i use the Select(0,0); it starts at 1, leaving a space behind. My mask is "000-000-0000"