C# 如何在没有选择开始的情况下设置文本框光标位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15138165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:13:06  来源:igfitidea点击:

How to set TextBox cursor position without SelectionStart

c#winforms.net-2.0

提问by walruz

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.

我有一个 Windows 窗体文本框,后台线程每秒更新其值。如果我将光标放在文本框中,它将在下次更新时失去当前位置。与文本选择相同。

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.

大多数情况下它运行良好。这是它不起作用的情况:
1) 我将光标放在文本框中文本的末尾
2) 按 SHIFT 并使用 <- 箭头键将光标向左移动
选择将无法正常工作。

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

它看起来像组合SelectionStart=10SelectionLength=1自动将光标移动到位置 11(不是我想要的 10)。

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.

如果有什么我可以做的,请告诉我!我正在使用 Framework.NET 2.0。
必须有一种方法可以在文本框中设置光标位置,而不是SelectionStart+SelectionLength.

采纳答案by walruz

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.

现在它的工作就像它应该的那样。

回答by Stefano Altieri

//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();

回答by S.Sathish Kumar

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  */