Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

September 21, 2009

Passing Data Between Forms: Constructor

Filed under: C#,VB.NET,WinForms @ 6:14 pm

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!

6 Comments

  1.   Munish — June 28, 2011 @ 4:13 am    Reply

    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

  2.   DeborahK — June 29, 2011 @ 1:14 pm    Reply

    Hi Munish –

    Add a public property in Form2 that Form1 can set.

    Hope this helps.

  3.   Bob B. — June 30, 2011 @ 12:25 pm    Reply

    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

  4.   muhammad fahad — September 29, 2011 @ 4:25 am    Reply

    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……

  5.   Bikithalee — July 1, 2014 @ 1:03 am    Reply

    You should include the InitializeComponent(); in the constructor calling.

    public Form2(string title,string txt)
    {
    InitializeComponent();
    this.Text = title;
    label1.Text = txt;
    }

    Source:

    http://net-informations.com/q/faq/passvalues.html

    bikitha

  6.   DeborahK — July 1, 2014 @ 11:00 am    Reply

    Hi Bikitha –

    Thank you for your comment.

    Both of the constructors in my example *do* call InitializeComponent. So I’m not sure what you are recommending that is different from what I have?

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2019 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs