【问题标题】:RichTextBox auto scroll to bottom when replacing a lineRichTextBox 替换行时自动滚动到底部
【发布时间】:2017-03-06 19:59:30
【问题描述】:

我正在使用richtexbox 为我的应用程序放置日志。我还将它用作后台工作人员的某种进度条。

我不打算详细介绍,但应用程序循环通过多个设备并收集数据,当前正在处理的数据显示在 Richtextbox 的最后一行,并且每次处理新数据时都会被替换。

这是日志的一个小样本。

Found 239 record(s), Transferred 239 record(s) from: 10.10.10.10 - 10/24/2016 7:37:45 PM
Found 42 record(s), Transferred 42 record(s) from: 10.10.10.11 - 10/24/2016 7:37:58 PM
43593...

在处理时,最后一行将被其他一些数据替换。直到它完成并再次像这样更换。

Found 239 record(s), Transferred 239 record(s) from: 10.10.10.10 - 10/24/2016 7:37:45 PM
Found 42 record(s), Transferred 42 record(s) from: 10.10.10.11 - 10/24/2016 7:37:58 PM
Found 2 record(s), Transferred 2 record(s) from: 10.10.10.12 - 10/24/2016 7:38:03 PM
986035...

我使用AppendText 将数据添加到richtextbox,这可以正常工作,并在我添加新记录时将richtextbox 滚动到底部。但是对于我的“进度条”,我将最后一行替换为:

Me.rtbLogs.Rtf = Me.rtbLogs.Rtf.Replace(Me.rtbLogs.Lines(rtbLogs.Lines.Length - 1), e.UserState.ToString())

一切正常,除了在替换文本时,richtextbox 会滚动回顶部。

【问题讨论】:

标签: vb.net winforms richtextbox


【解决方案1】:

您需要滚动到文本的末尾。试试这个:

  ' Append the new text and a new line
 RichTextbox1.AppendText("New Text" & vbNewLine)
  ' Sets the starting point of the selection         
RichTextbox1.SelectionStart = Len(RichTextbox1.Text)
  ' Scrolls to the caret
RichTextbox1.ScrollToCaret()
  ' Select the range 
RichTextbox1.Select()

【讨论】:

    猜你喜欢
    • 2010-10-28
    • 2014-11-03
    • 1970-01-01
    • 2016-11-13
    • 2014-12-12
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    相关资源
    最近更新 更多