Passing Data Between Forms: Constructor

Posted Mon, Sep 21 2009 16:14 by Deborah Kurata

It is often necessary for one form in your WinForms application to pass information to another form in your application. Though there are multiple ways to pass the data, this post focuses on using a form constructor.

Take a simple case, on Form1 there is a TextBox1 where the user enters data and clicks Button1. When the button is clicked, Form2 is displayed. Form2 needs the data that was entered on Form1.

To try this:

1. Create a form. Form1 needs a TextBox and button and described above.

2. Create a second form. You can add a label or other textbox if you want to confirm that the data was passed to this form.

3. Open the code editor for Form2.

4. Insert the following code.

In C#:

public partial class Form2: Form
{
    // Variable to store the passed in text
    string passedInText;

    public Form2(string text)
    {
        InitializeComponent();

        this.passedInText= text;
    }
}

In VB:

Public Class Form2

    ' Variable to store the passed in text
    Dim passedInText As String

    Public Sub New(ByVal text As String)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.passedInText= text
    End Sub
End Class

This code defines a variable to store the passed in text (passedInText).

The text is passed in on the form's constructor. In C#, the constructor is named using the name of the class, Form2 in this case. In VB, the constructor is defined using a subroutine named New. In either case, the parameter defines the text that is passed in.

Within the constructor, the code assigns the passed in value to the defined variable. So passedInText then refers to the text that was passed in to this form.

You can use the passedInText variable to assign the passed in text to a TextBox or Label on Form2 if you want to see the result. Otherwise, you can view the passedInText using the debugger or Output window.

So now Form2 is ready to receive the passed in text.

5. Open the code editor for Form1.

6. In the Button1 Click event, insert the following code.

In C#:

private void Button1_Click(object sender, EventArgs e)
{
            Form2 frmForm2 = new Form2(textBox1.Text);
            this.Hide();
            frmForm2 .ShowDialog();
            this.Show();
}

In VB:

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

            Dim frmForm2 As New Form2 (TextBox1.Text)
            Me.Hide()
            frmForm2 .ShowDialog()
            Me.Show()
End Sub

This code creates an instance of the new form, passing in the text from the TextBox. This passes the defined value to the constructor in Form2.

It then hides itself and shows the new form as a modal dialog. After the user returns from the modal dialog, the first form appears again. You can change this logic as required by your application, as long as the first line that passes the Textbox value remains the same.

Use this technique any time you need to pass a set of data from one form to another.

Enjoy!

Filed under: , , , ,

Comments

# Write a Memorization Helper Application

Monday, September 21, 2009 7:24 PM by Deborah's Developer MindScape

Though this is not necessarily a common requirement, this post demonstrates the following techniques

# Write a Memorization Helper Application

Monday, September 21, 2009 7:31 PM by Deborah's Developer MindScape

Though this is not necessarily a common requirement, this post demonstrates the following techniques

# re: Passing Data Between Forms: Constructor

Tuesday, June 28, 2011 4:13 AM by Munish

Hi,

Great info, but what to be done if we want to pass a variable to Form1 FROM Form2, that is, in reverse direction? I guess constructor cant be used since Form2 would already be loaded...?

Thanks in advance.

-Munish

# re: Passing Data Between Forms: Constructor

Wednesday, June 29, 2011 1:14 PM by Deborah Kurata

Hi Munish -

Add a public property in Form2 that Form1 can set.

Hope this helps.

# re: Passing Data Between Forms: Constructor

Thursday, June 30, 2011 12:25 PM by Bob B.

Hi Deborah,

As a Visual Studio newbee, I was wondering if it is possible to use this same method to pass multiple strings of data (ex:  the contents of TextBox1 AND TextBox2, etc) from one form to another?  If so, would you mind providing a VB example based on the above?

Thanks in advance.

Bob

# re: Passing Data Between Forms: Constructor

Thursday, September 29, 2011 4:25 AM by muhammad fahad

hi iam fahad i want to pass two variable through constructorscan u send me code above code is pass single.E-mail is:muhammadfahad90@yahoo.com

thnks alots......

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: