我如何自动滚动到多行文本框的底部? [英] How do I automatically scroll to the bottom of a multiline text box?

查看:759
本文介绍了我如何自动滚动到多行文本框的底部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与.Multiline属性设置为true的文本框。每隔一段时间,我加入到它的文本换行。我想文本框自动滚动到一旦增加新行最底层的条目(最新的)。我如何做到这一点?


解决方案

  

每隔一段时间,我添加文本的新行吧。我想文本框自动滚动到一旦增加新行最底层的条目(最新的)。


如果您使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.appendtext.aspx\"><$c$c>TextBox.AppendText(string文) ,它会自动滚动到新追加的文本的末尾。它避免了,如果你在一个循环中调用它闪烁的滚动条。

这也恰好是一个数量级比串联到的。文本属性更快。尽管这可能取决于你如何经常调用它;我是用一个紧密循环测试。


这不会滚动,如果它被称为显示文本框中之前,或者如果文本框,否则是不可见的(例如在的TabPanel不同的标签)。见<一href=\"http://stackoverflow.com/questions/18260702/textbox-appendtext-not-autoscrolling\">TextBox.AppendText()不自动滚动。这可能是也可能不是重要的,这取决于如果需要自动滚屏,当用户不能看到文本框上。

似乎从其他的答案的替代方法,也不会在这种情况下工作。它周围的一种方法是在 VisibleChanged 事件进行额外的滚动:

  textBox.VisibleChanged + =(发件人,E)=&GT;
{
    如果(textBox.Visible)
    {
        textBox.SelectionStart = textBox.TextLength;
        textBox.ScrollToCaret();
    }
};


在内部, AppendText通过确实是这样的:

  textBox.Select(textBox.TextLength + 1,0);
textBox.SelectedText = textToAppend;

不过应该​​没有理由这样做手工。

(如果你自己编译它,你会看到它使用了一些可能更有效的内部方法,并且有什么似乎是一个小特例。)

I have a textbox with the .Multiline property set to true. At regular intervals, I am adding new lines of text to it. I would like the textbox to automatically scroll to the bottom-most entry (the newest one) whenever a new line is added. How do I accomplish this?

解决方案

At regular intervals, I am adding new lines of text to it. I would like the textbox to automatically scroll to the bottom-most entry (the newest one) whenever a new line is added.

If you use TextBox.AppendText(string text), it will automatically scroll to the end of the newly appended text. It avoids the flickering scrollbar if you're calling it in a loop.

It also happens to be an order of magnitude faster than concatenating onto the .Text property. Though that might depend on how often you're calling it; I was testing with a tight loop.


This will not scroll if it is called before the textbox is shown, or if the textbox is otherwise not visible (e.g. in a different tab of a TabPanel). See TextBox.AppendText() not autoscrolling. This may or may not be important, depending on if you require autoscroll when the user can't see the textbox.

It seems that the alternative method from the other answers also don't work in this case. One way around it is to perform additional scrolling on the VisibleChanged event:

textBox.VisibleChanged += (sender, e) =>
{
    if (textBox.Visible)
    {
        textBox.SelectionStart = textBox.TextLength;
        textBox.ScrollToCaret();
    }
};


Internally, AppendText does something like this:

textBox.Select(textBox.TextLength + 1, 0);
textBox.SelectedText = textToAppend;

But there should be no reason to do it manually.

(If you decompile it yourself, you'll see that it uses some possibly more efficient internal methods, and has what seems to be a minor special case.)

这篇关于我如何自动滚动到多行文本框的底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆