Hi All,
Quick stupid qestion but how do you get values back and forth between forms. I will create the form on a button click enter/caluate three values exit the form. I need then to send the values back to first form (which will be disabled) Is there a good example of this as I have never really used a second form.
Glenn

2 answers
There are many ways to transfer values between forms.
Here's a straightforward way you could use which assumes the values are of different types - if they're of the same type you could use an array property rather than three scalar properties:
answered one year ago by:
17279
329
Umm, Thanks. One problem solved one created, I just have a simple Windows app at the moment, I'm trying to get the process working then assimilate it but have" Error 1 'WindowsFormsApplication2.Form2.Value1' is inaccessible due to its protection level C:\Documents and Settings\glennp\My Documents\Play\MultiForm\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 47 16 WindowsFormsApplication2" errors for all three of my variables....
17279
The properties need to be declared as internal (not 'private' which is the default) so that they will accessible from anywhere within the application.
329
Well made them internal seemed to get further now giving me {"Object reference not set to an instance of an object."} I think its the f2 value screwing things but... also can you give me a pointer to a web address I need to read more on multiple forms.... Glenn
17279
If the properties are on Form2 and there's an instance of Form2 open (even if it's hidden or disabled), then the line you need is: Form2 f2 = (Form2)Application.OpenForms["Form2"]; However, the code won't work if an instance of Form2 doesn't currently exist. Although I've answered hundreds of questions on this topic in my time, I've never seen an online article about it, probably because there are such a large number of variations which come up in practice. But the principles are very simple, you need a reference to the form and the properties you're setting need to be accessible.
329
Thanks, I have had a look on MSDN / Code Project and the like for an example no luck though. Glenn
329
Found one http://www.codeproject.com/KB/cs/pass_data_between_forms.aspx No sooner I said can't than can! Glenn
This is how I always do it.
answered one year ago by:
2499
329
Look like a bit of fiddling and this might work! Thanks Foamy & Vulpes
329
Thanks Foamy, I now having passing integers back I do have the warning: Accessing a member on 'WindowsFormsApplication1.Form2.Field2' may cause a runtime exception because it is a field of a marshal-by-reference class. I am treating it as a "it don't matter Vis Studio being picky error" but is this a potential flaw in my code? Glenn
2499
Can you post the code that generates this warning?
329
Oooh sorry just saw your reply. It appears I have fixed it, so no more complaints from Vis Studio. Sadly I cannot clearly remember what I did, I think I just altered the variable declaration to m
329
It has started again!! Some test code as I am a little nervous of adding it to my App before the wavy green line goes its in Vis Studio 2008 in a button click on Form1: textBox3.Text = f2.Value1.ToString(); textBox4.Text = f2.Value2.ToString(); textBox5.Text = f2.Value3.ToString(); three text box get values sent by Form2: in a button click again this.Value1 = Convert.ToInt16(label2.Text); this.Value2 = Convert.ToInt16(label3.Text); this.Value3 = Convert.ToInt16(label4.Text); this.Close(); I'm sending values around hence ints be used. Glenn
2499
The code is difficult to read in a comment. Can you paste the entire method into a new Answer?