简体   繁体   English

如何将文本从VB.NET Winform中的另一个类追加到richTextBox?

[英]How to append text to the richTextBox from another class in VB.NET Winform?

I've got a winform named Form1.vb and a class named FZConsole.vb On the winform I've got a Richtextbox1 and I'm trying to append text to it from the class but it won't work. 我有一个名为Form1.vb的winform和一个名为FZConsole.vb的类。在winform上,我有一个Richtextbox1,我试图将文本附加到该类中,但它不起作用。

From the Form1.vb I can easily do 从Form1.vb,我可以轻松地执行

Richtextbox1.AppendText("Console is currently ON.")

In the class I'm trying to do this, but it won't work: 在课堂上,我尝试执行此操作,但是它不起作用:

Class FZConsole
Public Sub FZConsole()
    While True
            Form1.Richtextbox1.AppendText(Environment.NewLine & "Test From Class")

        Threading.Thread.Sleep(1)
    End While
End Sub
End Class

When accessing controls from another thread you must typically invoke the accessing/updating of the control. 从另一个线程访问控件时,通常必须调用控件的访问/更新。 This is done to synchronize the updating of the controls, so that two threads does not update one control at the same time. 这样做是为了同步控件的更新,以使两个线程不会同时更新一个控件。

You can think of it like this: two people cannot write at the same paper at the same time. 您可以这样想:两个人不能同时写同一篇论文。

Invocation is usually nothing hard. 调用通常并不难。 It's basically just doing these two things: 基本上,这只是做这两件事:

  1. Check if invocation is required by the control or it's container. 检查控件或其容器是否需要调用。
  2. Invoke if necessary. 必要时调用。

Invocation is performed using Delegate methods. 调用是使用Delegate方法执行的。 If you target .NET Framework 4.0 or higher you can use the Sub() lambda expression to create a very simple delegate. 如果目标是.NET Framework 4.0或更高版本,则可以使用Sub() lambda表达式来创建一个非常简单的委托。

Public Sub FZConsole()
    While True
        If Form1.InvokeRequired = True Then 'Invocation is required.'
            Form1.Invoke(Sub() Form1.Richtextbox1.AppendText(Environment.NewLine & "Test From Class"))
        Else 'Invocation is not required.'
            Form1.Richtextbox1.AppendText(Environment.NewLine & "Test From Class")
        End If

        Threading.Thread.Sleep(1)
    End While
End Sub

However if you target .NET Framework 3.5 or lower things become a little trickier. 但是,如果您以.NET Framework 3.5或更低版本为目标,则有些麻烦。 When targeting an earlier framework you have to declare the delegate yourself, and it's not as simple as using the lambda since it works differently when passing variables. 以较早的框架为目标时,您必须自己声明委托,这并不像使用lambda那样简单,因为在传递变量时,lambda的工作方式有所不同。

Delegate Sub TextUpdaterDelegate(ByVal Text As String)

Public Sub FZConsole()
    While True
        If Form1.InvokeRequired = True Then 'Invocation is required.'
            Form1.Invoke(New TextUpdaterDelegate(AddressOf RichTextBox1.AppendText), Environment.NewLine & "Test From Class")
        Else 'Invocation is not required.'
            Form1.Richtextbox1.AppendText(Environment.NewLine & "Test From Class")
        End If

        Threading.Thread.Sleep(1)
    End While
End Sub
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim newInstanceToClass As New SomeClass
        RichTextBox1.AppendText(newInstanceToClass.returenText)
    End Sub


    Class SomeClass
        Function returenText() As String
            Dim txt As String = "some text..."
            Return txt
        End Function
    End Class
End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM