用全局变量在窗口之间传递值

时间:2013-12-08 00:37:23

标签: .net vb.net

所以我尝试使用Public变量将数据从Form1发送到Form2。我没有错误,但字符串“另一个测试”没有出现在Form1的文本框中。我这是正确的做法吗?

' ### Form1 ###
Public Class Form1
    Public MyData As String

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      MyData = "test"
    End Sub
End Class

' ### Form2 ###
Public Class Form2
    ' Form1
    Private Form1 As New Form1

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      If Form1.MyData = "test" Then
          Form1.Textbox1.text = "another test"
      End If
    End Sub
End Class

4 个答案:

答案 0 :(得分:1)

最好在Form1的构造函数中设置MyData = "test",而不是OnLoad事件。更好的方法是通过它的构造函数传递你想要Form2知道的值。这使得它更依赖于它的家属是谁而不是从静态/全球状态中拉出来。如果确实需要使用Form1然后传递表单,如果它只需要字符串然后只传递字符串。总是给课程他们完成工作所需的最低限度。这使它们更松散地耦合,更容易测试和使用。


如果首先加载form1,这将是一种方法。原因是它允许您将现有的form1传递给form2构造函数。否则你正在创建一个我不认为你想做的新form1

Public Class Form2
    Form Form1

    Sub New(ByVal form1 as Form)    
       Form1 = form
    EndSub

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      If Form1.Data = "test" Then
          Form1.Textbox1.text = "another test"
      End If
    End Sub
End Class

Public Class App
    Sub Main
       Form1 f1 = new Form1();
       // display f1
       Form2 f2 = new Form2(f1);
       // display f2
    End Sub
End Class

答案 1 :(得分:1)

问题是你正在创建2个form1 clases,例如一个属于yourproject.form1,第二个是yourproject.form2.form1,因为你正在创建一个属于类形式2的新表单。 / p>

Public Class Form2
' Form1
Private Form1 As New Form1 ' in this line you are creating a new form1 class that belongs to form2

Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If Form1.MyData = "test" Then
      Form1.Textbox1.text = "another test"
  End If
End Sub
End Class

你有两个选择:

  1. 删除Private Form1 As New Form1

  2. 将此行Form1.Textbox1更改为nameofyourproject.form1,以便vb确切地知道您所说的1。

  3. 希望有所帮助

答案 2 :(得分:0)

1)提到的命名问题。如果VB不混淆,我

2)你制作一个新表格(新)但从未表现出来。 NEW运行,你可以在构造函数中传递信息。否则,您需要在Load中显示代码的表单以执行。

Public Class Form1
Public MyData As String

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
       Handles Me.Load
  MyData = "test"
End Sub
End Class

' ### Form2 ###
Public Class Form2
   ' Form1
    ' really bad naming
    'Private Form1 As New Form1
     Private frm1 As New Form1           ' only a little better

   Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
      Handles Me.Load

       frm1.Show  ' SHOW form1 so the LOAD runs, not just NEW

      If frm1 .MyData = "test" Then
          frm1 .Textbox1.text = "another test"
      End If
    End Sub
End Class

Bravo用于实现表单的实例化,但通常表单不应该与其他表单的控件混淆。换句话说,使用构造函数可以工作,但在这种情况下不行。如果你想让Form2从Form1中获取一些文本 - 谁创建了他。 Form1中:

Dim frm2 As New Form2("some data to pass")

在form2中:

    Public Sub New(txt As String)

    ' NEVER add code before the InitializeComponent call
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Label1.Text = txt

End Sub

答案 3 :(得分:0)

这可能是你想要的:

' ---------
Public Class Form1

    Private innerData As String
    Public Property MyData() As String
        Get
            Return innerData
        End Get
        Set(ByVal value As String)
            innerData = value
        End Set
    End Property

    Public Property TextData() As String
        Get
            Return TextBox1.Text
        End Get
        Set(ByVal value As String)
            TextBox1.Text = value
        End Set
    End Property

End Class

' ---------
Public Class Form2

    Private frm1 As New Form1

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        If frm1 IsNot Nothing Then
            frm1.Show()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        frm1.MyData = "test"
        frm1.TextData = "another test"
    End Sub
End Class

result