-1

From the main Window Form I open a second one where I can write 2 values. I need those 2 values in the main form.

At the moment I open the second form using

NuovoForm.show()

where NuovoForm is the second form's name. The second form has 2 text fields and a button, how do I get in the first form the text written inside the 2 fields when the button is pushed?

Nakul Chaudhary
  • 25,572
  • 15
  • 44
  • 47
Luigi Caradonna
  • 1,044
  • 2
  • 12
  • 33
  • 2
    Srsly, there are hundreds of tutorials out there - [here](http://www.codeproject.com/Articles/12214/Passing-Values-between-Forms-in-NET-x-with-C-and), [here](http://stackoverflow.com/questions/4587952/passing-data-between-forms),etc. Just google it! – Eminem Jan 13 '16 at 12:32

4 Answers4

0

Simply create two properties in NuovoForm form and set these from the textboxes of NuovoForm. And get these values in main form through these new properties.

Nakul Chaudhary
  • 25,572
  • 15
  • 44
  • 47
0

One course of action is setting the Owner of the child form to the caller, in this case your main form. So in the child form we would use something like this were we update the main form's TextBox1 and two public properties in the main form.

    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

Child form

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

Note there is a good deal of code here, it does other things besides what you asked for as it's something I did several years ago but is relevant in that it shows how to interact with parent to child forms. Now if we did this in C# a few more things would need to happen so this for now is a vb.net thing while anyone wants it in C# I can do that also.

Anyways hope this helps

Karen Payne
  • 4,341
  • 2
  • 14
  • 31
0

Suppose the name of the 1st form is 'Form1', and the controls on 'NuovoForm' are 'TextBox1', 'TextBox2', and 'Button1'

You can use the following code:

Form1 code:

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

NuovoForm code:

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

Now, you can use Value1 and Value2 to do whatever you want in Form1

  • Exactly, glad someone saved me from posting this... This is the only answer the OP is looking for. – Arazio Jan 13 '16 at 15:16
-1

Another option is to show your child form as a dialog, for example:

    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

In your child form, close the form by putting the following line in your "Close" button:

    DialogResult = Windows.Forms.DialogResult.OK

Once the form is closed with the "OK" dialog result it will go into the If loop where you can extract the data with public properties. This will also give you a lot of control regarding when the data is retrieved, for example not retrieving the data when the form was prematurely closed.

GertV
  • 54
  • 7