vb 网络线程/类问题

发布于 2024-11-02 03:52:18 字数 1566 浏览 0 评论 0原文

大家好,我已经发布了有关此问题的文章,并且使用了答案来帮助我,但我仍然陷入困境,我真的要克服这个问题并继续我的程序的其余部分。我正在尝试使用另一个线程上另一个类的新文本更新文本框。我可以让它在同一类的另一个线程上以及另一个类的同一线程上毫无问题地工作。但不在另一个类的另一个线程上。我的代码看起来像这样,当它运行时不产生错误但不将测试添加到文本框中,奇怪的是我对程序进行了一些更改以将多行添加到 consolebox.text 然后检查 console.text来自 form2 并生成我添加的值,但由于某种原因这些值没有反映在控制台框中。有人请帮助我,这样我就可以不再打扰你了...哈哈

Public Class Form1
Dim myform2 As New form2
Dim t As New System.Threading.Thread(AddressOf myform2.display)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

Private Delegate Sub ConsoleDelegate(ByVal message As String)
Public Sub console(ByVal message As String)
    If Me.InvokeRequired Then
        Me.Invoke(New ConsoleDelegate(AddressOf console), New Object() {message})
        Return
    End If
    If Me.consolebox.Text.Length > 0 Then
        If Me.consolebox.Text.Substring(Me.consolebox.Text.Length - 3, 3) = "..." Then
            Me.consolebox.AppendText(message)
        ElseIf Me.consolebox.Text.Substring(Me.consolebox.Text.Length - 1, 1) = "." Then
            Me.consolebox.AppendText(ControlChars.NewLine & timestamp() & message)
        End If
    Else
        Me.consolebox.AppendText(timestamp() & message)
    End If
End Sub

Function timestamp()
    Return "[DATE]"
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    t.Start()
End Sub

结束课程

公共课程 form2

Public Sub display()
    Form1.console("hello")
End Sub

结束课程

Hi guys ive already posted about this and ive used the answers to help me, but im still stuck and i really to get past this and move onto the rest of my program. I'm trying to update a textbox with new text from another class on another thread. I can get this to work no problem on another thread with the same class, and on the same thread in another class. but not on another thread on another class. my code looks like this, when it is run to produces no errors but does not add the test to the text box, what is wierd is i had made some changes to the program to add multiplelines to consolebox.text and then checked console.text from form2 and it produces the values i added but those arent reflected inside in the consolebox for some reason. Someone please help me so i can stop bothering you... lol

Public Class Form1
Dim myform2 As New form2
Dim t As New System.Threading.Thread(AddressOf myform2.display)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

Private Delegate Sub ConsoleDelegate(ByVal message As String)
Public Sub console(ByVal message As String)
    If Me.InvokeRequired Then
        Me.Invoke(New ConsoleDelegate(AddressOf console), New Object() {message})
        Return
    End If
    If Me.consolebox.Text.Length > 0 Then
        If Me.consolebox.Text.Substring(Me.consolebox.Text.Length - 3, 3) = "..." Then
            Me.consolebox.AppendText(message)
        ElseIf Me.consolebox.Text.Substring(Me.consolebox.Text.Length - 1, 1) = "." Then
            Me.consolebox.AppendText(ControlChars.NewLine & timestamp() & message)
        End If
    Else
        Me.consolebox.AppendText(timestamp() & message)
    End If
End Sub

Function timestamp()
    Return "[DATE]"
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    t.Start()
End Sub

End Class

Public Class form2

Public Sub display()
    Form1.console("hello")
End Sub

End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-11-09 03:52:18

您必须将 Form1 的实例传递给 form2:

Dim myform2 As New form2(me)

和 form2:

Public class form2
    Dim _form1 As Form1

    Public New(form1 As Form1)
        _form1 = form1
    End Sub

    Public Sub Display()
        _form1.console("hello")
    End Sub
End Class

You have to pass the instance of Form1 to form2:

Dim myform2 As New form2(me)

and form2:

Public class form2
    Dim _form1 As Form1

    Public New(form1 As Form1)
        _form1 = form1
    End Sub

    Public Sub Display()
        _form1.console("hello")
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文