Passing values from form to form

Posted on 2006-03-25
Last Modified: 2010-04-16
I have 3 textboxes on Form1 which a user inputs values (x,y,z).

On Form2, I would like to use those values.

I create properties in Form1 as follows:

 public int UserValueX
            get { return x = Int16.Parse(txtX.Text); }

public int UserValueY
            get { return y = Int16.Parse(txtY.Text); }

 public int UserValueZ
            get { return z = Int16.Parse(txtZ.Text); }

Form2 is launched by clicking a button on Form1.

On Form2, I use the following code to retrieve the values (x,y,z) from Form1.

// Get x, y and z values from Form1 User Input

Form1 frm = new Form1();
x = frm.UserValueX;
y = frm.UserValueY;
z = frm.UserValueZ;

I have the default Text properties of x,y,z set to 4 before I start the application.  When I change x,y, or z on Form1, and use the button to launch Form2, I recieves the default Text property values instead of the ones I have changed them to.

Can someone point out to me what I am doing wrong?

Question by:rivusglobal
    LVL 85

    Accepted Solution

    Why not make x, y and z public properties on Form2?  Then you can create your instance of Form2 and set the values before displaying the form:

        // from the Button click event in Form1...
        Form2 frm2 = new Form2();
        frm2.UserValueX = Int16.Parse(this.txtX.Text);
        frm2.UserValueY = Int16.Parse(this.txtY.Text);
        frm2.UserValueZ = Int16.Parse(this.txtZ.Text);
    LVL 10

    Author Comment

    Thanks Idle. That did the trick.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now