第二种形式在调用时不显示以第一种形式存储的值

时间:2012-01-19 08:43:42

标签: vb.net winforms

嘿所有我想弄清楚为什么我的第二张表格没有显示我在第一张表格中收到的价值。

第一种形式的代码是:

 Private Sub scannerOnCom_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
    responceBack = scannerOnCom.ReadLine
    Call frm1110.clickButton(responceBack) 
 End Sub

第二种形式代码是:

Public Sub clickButton(ByRef theResponse As String)
    txtNumber.Text = theResponse
    'Call cmdNextFinish_Click(Nothing, Nothing)
End Sub

然而,当我调试它以确保为响应时存储了某些内容时,但由于某种原因它没有将它放入文本框中。它是空白的。

任何帮助都会很棒!

大卫

更新

好吧Form1:

Dim tmpForm3020 As New frm3020

Private Sub cmd3020_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3020.Click
    tmpForm3020.Show()
    Me.WindowState = FormWindowState.Minimized
End Sub

Private Sub scannerOnCom_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
    responceBack = scannerOnCom.ReadLine
    tmpForm3020.txtNumber.Text = responceBack
End Sub

如果那是正确的,那么我就会在网上收到错误:

xForm.txtNumber.Text = responceBack

话说:

跨线程操作无效:控制'txtNumber'从其创建的线程以外的线程访问。

2 个答案:

答案 0 :(得分:1)

Dim tmpForm3020 As New frm3020

Private Sub cmd3020_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3020.Click
    tmpForm3020.Show()
    Me.WindowState = FormWindowState.Minimized
End Sub

Private Sub scannerOnCom_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
    responceBack = scannerOnCom.ReadLine
    TestData(responceBack)
End Sub

Private Sub TestData(ByVal xVal As String)
   If InvokeRequired Then
      Me.Invoke(New MethodInvoker(AddressOf TestData))
      ' change Me to tmpForm3020 (if it does not work)
      ' tmpForm3020.Invoke(New MethodInvoker(AddressOf TestData))  
   Else
      tmpForm3020.txtNumber.Text = xVal
   End If
End Sub

答案 1 :(得分:1)

您是明确创建第二个表单的实例,还是依赖于默认实例?即是“frm1110”第二种形式的类名,还是你刚刚创建的实例?在任何一种情况下都要确保它与实际显示的实例相同。