C# 如何不断滚动到多行文本框中的文本末尾?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8917067/
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-09 05:09:56  来源:igfitidea点击:

How to constantly scroll to the end of text in multiline text box?

c#winformstextbox

提问by HelpNeeder

I'm updating my text box with text using a timer. Each time timer ticks I'm being redirected to the beginning to the text typed in my multiline text box.

我正在使用计时器用文本更新我的文本框。每次计时器滴答作响时,我都会被重定向到多行文本框中键入的文本的开头。

How to do this?

这该怎么做?

采纳答案by Kotch

I'd say that when you refresh, you could move the selection cursor to the end, then scroll the textbox 'til it's visible using ScrollToCaret.

我想说的是,当你刷新时,你可以将选择光标移动到最后,然后滚动文本框直到它使用 ScrollToCaret 可见。

That'll be something like

那会是这样的

 yourtextbox.SelectionStart = yourtextbox.Text.Length
 yourtextbox.ScrollToCaret()

回答by John Jeffery

Try using the TextBox.Selectmethod:

尝试使用以下TextBox.Select方法:

textBox.Select(textBox.Text.Length, 0);

That will set the cursor to just past the last character in the text box.

这会将光标设置为刚好经过文本框中的最后一个字符。

回答by HelpNeeder

This works much better. It's better than Kotch's solution because there is no need constantly updating the position of cursor.

这工作得更好。它比 Kotch 的解决方案更好,因为不需要不断更新光标的位置。

txtDisplay.AppendText(txtDisplay.SelectedText);