【问题标题】:c# string.replace scroll bar goes to topc# string.replace 滚动条到顶部
【发布时间】:2016-04-13 04:14:37
【问题描述】:

我有一个多行文本框,当我替换它的文本时,滚动条会转到顶部...

我怎样才能防止这种情况发生并将滚动条保持在底部,就像文本框中的 AppendText 方法一样?

我的代码如下:

int milisecond = 100;
int persent = 0;
do
{
    Random rnd = new Random();
    int add = rnd.Next(1, 9);
    int wait = rnd.Next(50, 1000);
    textBox1.Text = textBox1.Text.Replace("Progress %" + persent, "");
    persent += add;
    if (persent > 100)
    {
        persent = 100;
    }

    textbox1.AppendText("Progress %" + persent);
    Task.Delay(milisecond).Wait();

    this.Refresh();

} while (persent != 100);

【问题讨论】:

  • 知道进度条就是这样工作的! :oP 出于兴趣,这是一个 Windows 窗体应用程序吗?
  • Windows 窗体,但类似于控制台应用程序:P ...
  • 据我所知,代码可能在与 UI 相同的线程上执行。因此,您尝试使用 Refresh() 方法强制屏幕更新。我建议使用BackgroundWorker 来帮助您进行代码/ UI 任务的线程管理。这里解释一下:dotnetperls.com/backgroundworker
  • 哦,谢谢! ,我删除了 this.Refresh() 并工作:D,再次感谢
  • 没问题 - 如果将来您希望同时运行代码和更新 UI(我认为这是进度指示器),那么我强烈推荐 BackgroundWorker - 它是一种非常方便的方法管理这种类型的线程注意事项。

标签: c# string winforms replace


【解决方案1】:

在 AppendText() 之后添加这个

        textbox1.ScrollToCaret();

【讨论】:

猜你喜欢
  • 2010-09-21
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
相关资源
最近更新 更多