怪异的VB.net向其他窗体显示文本行为

怪异的VB.net向其他窗体显示文本行为,vb.net,winforms,Vb.net,Winforms,大家好,我有一个程序,我想把我的form1的文本显示到我的form3中,其中form2有执行命令的按钮,调试中没有错误,但是当我点击我的form2中的按钮时,我想要的form1中的form3上的数据不在那里。请帮我,伙计们,提前谢谢 表格1代码: Public Class Form1 Private frm2 As Form2 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs

大家好,我有一个程序,我想把我的form1的文本显示到我的form3中,其中form2有执行命令的按钮,调试中没有错误,但是当我点击我的form2中的按钮时,我想要的form1中的form3上的数据不在那里。请帮我,伙计们,提前谢谢

表格1代码:

 Public Class Form1
Private frm2 As Form2
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    If frm2 Is Nothing Then
        frm2 = New Form2
        AddHandler frm2.FormClosed, AddressOf Me.Form2HasBeenClosed

        Dim Label21 As Label = New Label
        frm2.Label21.Text = TextBox1.Text
        frm2.Label21.ForeColor = Color.Black
     End If

    If frm2 IsNot Nothing Then
        frm2.Show(Me) 'Show Second Form  
        Me.Hide()
    End If

End Sub
表格2代码:

Public Class Form2
Private frm1 As New Form1
Private frm3 As Form3
Public lbl As New Label
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    Dim Label21 As Label = New Label
    Dim Textbox1 As TextBox = New TextBox
    frm3.Label21.Text = frm1.TextBox1.Text

        Form3.Show()


End Sub
End Class

您需要将Form1实例的引用传递给名为frm2的Form2实例。 当在Form2类代码中尝试显示Form3时,应使用该引用传递Form1初始实例中的值。相反,您的代码构建Form1 Form1 frm=new Form1的新实例,并尝试从此实例获取值。当然,作为Form1的新实例,frm1变量不包含原始实例中存在的任何值

Public Class Form2
    Private frm1 As Form1   ' remove the new here because it creates a new instance of Form1'
    Private frm3 As Form3
    Public lbl As New Label ' not needed?'

    ' add a constructor that receives the instance of the caller'
    Public Sub New(callerInstance as Form1)

        ' Call required if you add your constructor manually'
        InitializeComponent()

        ' save the instance of the Me variable passed to this constructor'
        frm1 = callerInstance
    End Sub

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

       ' Dim Label21 As Label = New Label          ' this label is not needed'
       ' Dim Textbox1 As TextBox = New TextBox     ' this  textbox is not needed'

       ' Create a new instance of Form3, do not use the automatic Form3 instance
       ' automatically created by VB.NET 
       frm3 = new Form3()

       ' now you are referring to the caller instance of Form1 '
       ' where there is the textbox filled with your text '
       frm3.Label21.Text = frm1.TextBox1.Text
       frm3.Show()

     End Sub
End Class
然后更改Fomr1类中以这种方式构建Form2实例的代码

Private Sub Button3_Click(...........)

    If frm2 Is Nothing Then
        frm2 = New Form2(Me)
        .....
此更改使用Form1 Me的当前实例调用在Form2的类代码中定义的构造函数,并且正是此实例包含稍后需要传递给frm3的文本框值

我不想光顾这里,但似乎您对类和类实例有点困惑。您需要很好地理解这一点,因为它是面向对象编程的基本支柱之一。VB自动创建表单对象在这里没有帮助


这里有一个非常基本的介绍:

先生,感谢您的帮助..我有一个问题Private Sub-NewcallerInstance,因为Form1在调试时犯了一个错误,我试图找出它,但我无法即时通讯,只是编程方面的新手,请帮助我,谢谢错误是在设计器生成的类型中警告2“Private Sub-NewcallerInstance as Form1”“WindowsApplication1.Form2”应调用InitializeComponent方法。先生抱歉,此指南不清楚,请帮助我如何通过代码合并初始化组件非常感谢先生许多感谢先生steve先生是的,它解决了问题,但出现了另一个问题错误1“WindowsApplication1.Form2.Private Sub NewcallerInstance As”“Form1”在此上下文中不可访问,因为它是“Private”。它在我的Form1上出错您好,先生。它创建了另一个错误错误1参数,未为“Public Sub NewcallerInstance As Form1”的参数“callerInstance”指定。感谢您,我已使用这些评论中出现的详细信息修改了答案。现在对未来的过路人来说也应该没问题