Vb.net 从第二个Windows窗体获取值

Vb.net 从第二个Windows窗体获取值,vb.net,Vb.net,在主窗口窗体中,我打开了第二个窗体,可以在其中写入2个值。 我需要这两个值在主窗体中 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Me.Owner IsNot Nothing Then Dim MainForm As Form1 = CType(Me.Owner, Form1) MainForm.T

在主窗口窗体中,我打开了第二个窗体,可以在其中写入2个值。 我需要这两个值在主窗体中

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Me.Owner IsNot Nothing Then
            Dim MainForm As Form1 = CType(Me.Owner, Form1)

            MainForm.TextBox1.Text = Me.TextBox1.Text
            MainForm.Item1 = 1
            MainForm.Item2 = "Hello, made this change in child form"
        Else
            Console.WriteLine("Owner not set")
        End If
    End Sub

In the main form we call the child form

    mChildForm = New ChildForm
    mChildForm.Owner = Me
    mChildForm.Show()

I have a complete `demonstration project` on Microsoft OneDrive or see code below

Main form
Public Class Form1
    Private mChildForm As ChildForm
    Private mFirstTime As Boolean = True
    Public Property Item1 As Integer
    Public Property Item2 As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DoFormWork()
    End Sub
    Private Sub DoFormWork()
        If Not ((From f In My.Application.OpenForms.Cast(Of Form)() Where f.Name.Equals("ChildForm") Select f.Name).ToList.Count > 0) Then
            mChildForm = New ChildForm
            mChildForm.Owner = Me
            mFirstTime = True
        End If
        mChildForm.Show()
        If mFirstTime Then
            mChildForm.Location = New Point(Me.Left + Me.Width, Top)
            mFirstTime = False
        End If

        If chkPushText.Checked Then
            mChildForm.TextBox1.Text = Me.TextBox1.Text
        End If
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If mChildForm IsNot Nothing Then
            mChildForm.Dispose()
        Else
            Console.WriteLine("mChildForm is Nothing")
        End If
    End Sub
    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown

        Controls.OfType(Of Control).ToList.ForEach(
            Sub(x)
                If TypeOf x Is TextBox Then
                    AddHandler x.Click,
                        Sub(s As System.Object, a As System.EventArgs)
                            Dim tb As TextBox = CType(x, TextBox)
                            If Not String.IsNullOrEmpty(tb.Text) Then
                                DoFormWork()
                                mChildForm.TextBox1.Text = tb.Text
                            End If
                        End Sub
                End If
            End Sub)

        mChildForm = New ChildForm
        mChildForm.Owner = Me
        mChildForm.Show()
        mChildForm.Location = New Point(Me.Left + Me.Width, Top)
        mFirstTime = False
        My.Application.OpenForms(0).Activate()
        DoFormWork()
    End Sub
    Private Sub Form1_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
        If Not mFirstTime Then
            mChildForm.Location = New Point(Me.Left + Me.Width, Top)
        End If
    End Sub

End Class
目前,我使用

NuovoForm.show()

其中,NuovoForm是第二种形式的名称。第二个表单有两个文本字段和一个按钮,当按下按钮时,如何在第一个表单中获取写入两个字段中的文本?

只需在NuovoForm表单中创建两个属性,并从NuovoForm的文本框中设置它们。并通过这些新属性在主窗体中获取这些值。

一个操作过程是将子窗体的所有者设置为调用者,在本例中是主窗体。所以在子表单中,我们会使用类似这样的东西,更新主表单的TextBox1和主表单中的两个公共属性

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Me.Owner IsNot Nothing Then
            Dim MainForm As Form1 = CType(Me.Owner, Form1)

            MainForm.TextBox1.Text = Me.TextBox1.Text
            MainForm.Item1 = 1
            MainForm.Item2 = "Hello, made this change in child form"
        Else
            Console.WriteLine("Owner not set")
        End If
    End Sub

In the main form we call the child form

    mChildForm = New ChildForm
    mChildForm.Owner = Me
    mChildForm.Show()

I have a complete `demonstration project` on Microsoft OneDrive or see code below

Main form
Public Class Form1
    Private mChildForm As ChildForm
    Private mFirstTime As Boolean = True
    Public Property Item1 As Integer
    Public Property Item2 As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DoFormWork()
    End Sub
    Private Sub DoFormWork()
        If Not ((From f In My.Application.OpenForms.Cast(Of Form)() Where f.Name.Equals("ChildForm") Select f.Name).ToList.Count > 0) Then
            mChildForm = New ChildForm
            mChildForm.Owner = Me
            mFirstTime = True
        End If
        mChildForm.Show()
        If mFirstTime Then
            mChildForm.Location = New Point(Me.Left + Me.Width, Top)
            mFirstTime = False
        End If

        If chkPushText.Checked Then
            mChildForm.TextBox1.Text = Me.TextBox1.Text
        End If
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If mChildForm IsNot Nothing Then
            mChildForm.Dispose()
        Else
            Console.WriteLine("mChildForm is Nothing")
        End If
    End Sub
    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown

        Controls.OfType(Of Control).ToList.ForEach(
            Sub(x)
                If TypeOf x Is TextBox Then
                    AddHandler x.Click,
                        Sub(s As System.Object, a As System.EventArgs)
                            Dim tb As TextBox = CType(x, TextBox)
                            If Not String.IsNullOrEmpty(tb.Text) Then
                                DoFormWork()
                                mChildForm.TextBox1.Text = tb.Text
                            End If
                        End Sub
                End If
            End Sub)

        mChildForm = New ChildForm
        mChildForm.Owner = Me
        mChildForm.Show()
        mChildForm.Location = New Point(Me.Left + Me.Width, Top)
        mFirstTime = False
        My.Application.OpenForms(0).Activate()
        DoFormWork()
    End Sub
    Private Sub Form1_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
        If Not mFirstTime Then
            mChildForm.Location = New Point(Me.Left + Me.Width, Top)
        End If
    End Sub

End Class
子窗体

Public Class ChildForm
    Public Sub New()
        InitializeComponent()
    End Sub
    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Close()
    End Sub
    Private Sub cmdHideMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHideMe.Click
        Hide()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Me.Owner IsNot Nothing Then
            Dim MainForm As Form1 = CType(Me.Owner, Form1)

            MainForm.TextBox1.Text = Me.TextBox1.Text
            MainForm.Item1 = 1
            MainForm.Item2 = "Hello, made this change in child form"
        Else
            Console.WriteLine("Owner not set")
        End If
    End Sub
End Class
注意,这里有很多代码,除了您要求的之外,它还做其他事情,因为这是我几年前做的事情,但它的相关性在于它展示了如何与父到子表单交互。现在,如果我们在C#中这样做,还需要做一些事情,所以现在这是一个vb.net的东西,而任何人都想在C#中这样做,我也可以这样做


无论如何,希望这有帮助

另一个选项是将子窗体显示为对话框,例如:

    Dim frmDia As New NuovoForm
    frmDia.TopMost = True
    frmDia.StartPosition = FormStartPosition.CenterScreen
    If frmDia.ShowDialog = Windows.Forms.DialogResult.OK Then
        ' Get data here
    End If
在子窗体中,通过在“关闭”按钮中添加以下行来关闭窗体:


一旦表单以“OK”对话框结果关闭,它将进入If循环,您可以在其中提取具有公共属性的数据。这还将为您提供很多有关何时检索数据的控制,例如,在表单过早关闭时不检索数据。

假设第一个表单的名称为“
Form1
”,而“
NuovoForm
”上的控件为“
TextBox1
”、“
TextBox2
”和“
Button1

您可以使用以下代码:

表格1代码:

Public Class Form1
    Public Value1 As String, value2 As String
    '
    '
    '
End Class
Public Class NuovoForm

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Form1.Value1 = TextBox1.Text
        Form1.value2 = TextBox2.Text
        Me.Hide()
        Form1.Show()
    End Sub

End Class
新格式代码:

Public Class Form1
    Public Value1 As String, value2 As String
    '
    '
    '
End Class
Public Class NuovoForm

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Form1.Value1 = TextBox1.Text
        Form1.value2 = TextBox2.Text
        Me.Hide()
        Form1.Show()
    End Sub

End Class

现在,您可以使用Value1和Value2在
Form1

sry中执行任何您想执行的操作。此外,还有数百个教程,等等。只需通过谷歌搜索即可!没错,很高兴有人救了我的命。。。这是OP寻找的唯一答案。