Passing values from form to form

Posted on 2006-03-25
Medium Priority
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 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 16292196
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

ID: 16292237
Thanks Idle. That did the trick.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…
Did you know PowerShell can save you time with SaaS platforms? Simply leverage RESTfulAPIs to build your own PowerShell modules. These will kill repetitive tickets and tabs, using the command Invoke-RestMethod. Tune into this webinar to learn how…

619 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