TextBox.AppendText() 不自动滚动 [英] TextBox.AppendText() not autoscrolling

查看:37
本文介绍了TextBox.AppendText() 不自动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下方法让我的文本框文本自动滚动:

I tried the following to get my Textbox text to automatically scroll:

我使用的步骤非常简单:

The steps I am using are pretty trivial:

  1. 将文本框拖到表单上.
  2. 将文本框更改为多行.
  3. 添加垂直滚动.
  4. 使用 AppendText() 将文本添加到文本框.

尽管尝试了此处提到的解决方案,但文本不会自动滚动:

The text does not automatically scroll, despite trying to solutions mentioned here:

我该怎么做自动滚动到多行文本框的底部?

这是什么原因造成的,我该如何解决?

What could cause this and how do I fix it?

更新:如果我创建一个按钮并使用它来调用 AppendText() 我会得到所需的行为.但是,如果我尝试从表单的构造函数或 Load() 事件调用 AppendText,那么我会得到附加的文本,但 TextBox 不会滚动.这不是一个重复的问题,因为我过去没有看到有人发布过这个问题.

UPDATE: If I create a button and use it to call AppendText() I get the desired behavior. However, if I try to call AppendText from the form's constructor or Load() event then I get the appended text but the TextBox does not scroll. This is NOT a duplicate question as I haven't seen anyone post this problem in the past.

推荐答案

由于表单在构造函数和加载事件期间还没有完全准备好,我不得不使用一个任务让它在它准备好后滚动:

Since the form isn't quite ready during the constructor and load event, I had to use a task to get it to scroll after it becomes ready:

p>

这里是被调用的方法:

Here is the method that gets invoked:

void scroll()
{
    this.Invoke(new MethodInvoker(delegate()
        {
            textBox1.SelectionStart = textBox1.Text.Length;
            textBox1.ScrollToCaret();
        }));
}

它通过放置在加载事件中的这个任务被调用:

It gets invoked via this task placed in the load event:

Task task1 = new Task(new Action(scroll));
            task1.Start();

这篇关于TextBox.AppendText() 不自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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