我正在尝试访问另一个窗体上的富文本框,我使用以下代码进行操作:
Private Delegate Sub StringDelegateChat(text As String, window As ChatWindow)
Private Sub AppendTextChatWindows(text As String, window As ChatWindow)
Try
If window.RichTextBox1.InvokeRequired Then
window.Invoke(New StringDelegateChat(AddressOf AppendTextChatWindows), text, window)
Else
window.RichTextBox1.AppendText(text)
window.RichTextBox1.SelectionStart = window.RichTextBox1.Text.Length
window.RichTextBox1.ScrollToCaret()
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
但是我遇到了“跨线程操作无效”的错误,我认为这是因为它错过了if语句中的“window.invoke”部分。我还尝试将“If window.RichTextBox1.InvokeRequired Then”替换为“If InvokeRequired Then”,但它会陷入一个连续循环并引发堆栈溢出错误。
谢谢 霍拉汉