从第二个Windows窗体获取值

时间:2016-01-13 12:22:59

标签: vb.net

从主窗口窗口我打开第二个,我可以写2个值。 我需要在主窗体中使用这两个值。

目前我使用

打开第二个表单
NuovoForm.show()

NuovoForm是第二种形式的名字。第二种形式有2个文本字段和一个按钮,当按下按钮时,如何在第一个表格中输入2个字段内写的文字?

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

一个操作过程是将子表单的所有者设置为调用者,在本例中为主表单。所以在子格式中我们会使用这样的东西,我们更新主窗体的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#中想要它我也可以这样做。

无论如何希望这有帮助

答案 2 :(得分:0)

假设第一个表单的名称为“Form1”,“NuovoForm”的控件为“TextBox1”,“TextBox2”和“{ {1}}“

您可以使用以下代码:

Form1代码:

Button1

NuovoForm代码:

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

现在,您可以使用Value1和Value2在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

中执行任何操作

答案 3 :(得分:-1)

另一个选项是将您的子表单显示为对话框,例如:

    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

在您的子表单中,将以下行放在"关闭"按钮:

    DialogResult = Windows.Forms.DialogResult.OK

使用" OK"关闭表单后对话框结果它将进入If循环,您可以在其中提取具有公共属性的数据。这也将为您提供有关何时检索数据的大量控制,例如,在表单过早关闭时不检索数据。