在码农的生活中,很多级码农都有这样的经历,会被一个小小的技术问题拦住,然后进度跟不上了,被老板XXXX一大通了。心情不爽了。
好吧,这个曾经是我遇到拦路虎之一。但事实上不是什么大技术。技术就是一层纸,破了就破了。
这是一个关于如何跨窗体操作控件或过程的一个例子。比如,你想用窗体A的按键来执行窗体B的文本框变色。
Imports System
Imports System.Threading
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckForIllegalCrossThreadCalls = False '不写这行,会出错,不允许线程的数据写到TextBox1.Text 中去。
Form2.Show()
End Sub
Private Sub form1_FormClosing(sender As Object, e As EventArgs) Handles Me.FormClosing
' If runThread.IsAlive = True Then runThread.Abort()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'用的是委托方式
Me.Invoke(New MethodInvoker(AddressOf THREAD2))
End Sub
Private Sub THREAD2()
Static j As Integer
j = j + 1
TextBox1.Text = " 这是 [线程] 操作" & vbCrLf & _
" Button2被点了: " & j & " 次" & vbCrLf & "要求是from2.textbox.text= textbox1.text 。[问题]但为什么不能成功显示呢?"
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Form2.TextBox1.Text = TextBox1.Text
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Static j As Integer
j = j + 1
TextBox1.Text = " 这是 [非线程] 操作" & vbCrLf & _
"Button3 点击了: " & j & " 次" & vbCrLf & _
"要求是from2.textbox.text= textbox1.text, 可以成功显示,这个是对的。"
End Sub
End Class
1