TextBox.AppendText() 不自动滚动

124

本文介绍了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() 不自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14