【问题标题】:RichTextBox vertical auto scrolling textRichTextBox 垂直自动滚动文本
【发布时间】:2016-06-20 03:34:28
【问题描述】:

我需要构建一个显示公告的数字标牌应用程序。因为文本应该具有被设计为用户的能力,所以我想到了一个富文本框控件,文本会自动垂直滚动(如“新闻”文本框)。 有什么想法吗?

【问题讨论】:

  • 到目前为止的代码?您可以定期向富文本框发送 WM_SCROLL 消息。滚动到底部时会发生什么?使用 HTML 页面(可以处理从内部滚动,JavaScript)可能可以实现更好的控制,并使用 WebBrowser 控件显示在您的 VB.NET 程序中。

标签: vb.net richtextbox


【解决方案1】:

好的,我正在等火车的时候为你写这篇文章所以我还没有测试过,但是这样的东西应该可以解决问题。

注意
如果您收到CheckForIllegalCrossThreadCalls 错误
请勿将其设置为 CheckForIllegalCrossThreadCalls = False,除非出于调试目的。
虽然您一开始不会注意到任何事情,但拥有CheckForIllegalCrossThreadCalls = Falsecan 并且会在以后的轨道上引起问题。

说了这么多,下面是代码。

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    TextBox1.SelectionStart = 0 ''Change to a RichTextBox if you want.
    ''Starting the background worker.
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    ''BACKGROUND WORKER''
    For i = 0 To TextBox1.Text.Length
        TextBox1.SelectionStart = i
        System.Threading.Thread.Sleep(200)''changing the sleep time will make the textbox scroll to the right faster or slower.
    Next

End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    ''Once completed, The background worker will start again.
    TextBox1.SelectionStart = 0
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
    ''This should stop the program from crashing when closing if the background worker is still running
    BackgroundWorker1.CancelAsync()
End Sub
End Class

注意
您需要使用后台工作程序,否则您的程序将无法响应,直到 Loop 完成。
如果您再次收到 CheckForIllegalCrossThreadCalls 错误,请查看 Google 并搜索以解决此问题,但是,如果您愿意将其保留为 CheckForIllegalCrossThreadCalls = False,则由您决定。
让我知道你的进展,我没有测试过,但是由于10小时内没有人回复你,我想即使没有编译器来测试它我也会尝试一下。

【讨论】:

  • 嗨,我会试试这个,并尽快让你知道。谢谢!
  • 没问题,如果你碰巧遇到了我所说的那个错误,但很高兴能忍受它,直到你解决所有代码,这里是修复 CheckForIllegalCrossThreadCalls = False 将它添加到表单加载子
  • 你为什么不从一开始就让它成为线程安全的? :)
  • 哈哈!是的,你是对的,但是我在我糟糕的上网本上没有 Vb.net 的编译器,所以我不想犯错误,我至少可以帮助大声笑。话虽如此,当我长大后,我想成为一个行走的编译器哈哈哈!
  • @Kasper555 你过得怎么样?
猜你喜欢
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 2012-02-14
相关资源
最近更新 更多