将字符串从表单传递到新表单名称

时间:2017-06-25 07:23:37

标签: string vb.net forms winforms

谢谢大家的帮助。我有三个表单需要从NameForm输入的字符串名称传递给Form1并将Form1重命名为文本框上的任何名称。

然后在Form1中重命名为JOHN DOE,我将有各种文本框来输入数据。点击计算后,它会将所有数据传递给Form3并将Form3重命名为JOHN DOE。我已经编写了所有代码并且正常运行将数据从FORM1传递给Form3,除了它没有从NameForm传递学生名称并重命名Form1和Form3。下面是我的NameForm代码,它是输入学生姓名的基本形式。如果需要更多代码,我会发布它。我不想发布所有代码和风险投票。

Public Class NameForm

   Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Dim name As New Form1()
        name.StringPass = Form1.Text
        name.Show()

    End Sub

    Private Sub NameForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Focus()
        Me.BringToFront()
        Me.Hide()

    End Sub

End Class

2 个答案:

答案 0 :(得分:1)

我会在您要将值传递给的表单上实现New方法:

Form1

Public Class Form1

    Private Sub btnPass_Click(sender As Object, e As EventArgs) Handles btnPass.Click

        Dim form As New Form2(TextBox1.Text)
        form.Show()

    End Sub

End Class

Form2

Public Class Form2

    Public Sub New(ByVal value As String)

        ' This call is required by the designer.
        InitializeComponent()

        Label1.Text = value

    End Sub

End Class

截图:

enter image description here

答案 1 :(得分:-1)

您只需要向班级声明公共访问级别。 (感谢Enigmativity的更正)

Public Class NameForm
    Public studentName As String

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

         Dim name As New Form1()
         name.Text = studentName
         name.Show()

     End Sub

     Private Sub NameForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

         Me.Focus()
         Me.BringToFront()
         Me.Hide()

     End Sub

End Class

如果要从其他类调用全局变量: 例如以防您需要

Public Class Form3

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

        TextBox1.Text = NameForm.studentName

    End Sub

End Class