blah blah blah is here! blah blah » Close

up0down
link

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

last answered one year ago

2 answers

up1down
link

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:

// add these instance properties to Form1 (.NET 3.5 or 4.0 assumed)
internal int Value1 { get; set; }
internal double Value2 { get; set; }
internal string Value3 { get; set; }

// handle the FormClosing event in Form2 and add this code
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
// get a reference to Form1
Form1 f1 = (Form1)Application.OpenForms["Form1"];
// set properties
f1.Value1 = someInt;
f1.Value2 = someDouble;
f1.Value3 = someString;
// re-enable Form1
f1.Enabled = true;
}

GlennP
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....

vulpes
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.

GlennP
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

vulpes
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.

GlennP
329

Thanks, I have had a look on MSDN / Code Project and the like for an example no luck though. Glenn

GlennP
329

Found one http://www.codeproject.com/KB/cs/pass_data_between_forms.aspx No sooner I said can't than can! Glenn

up1down
link

This is how I always do it.

//in form 1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this);
textBox1.Text = f2.Field1;
f2.Dispose();
}


//in form 2
internal string Field1 { get; private set; }

private void button1_Click(object sender, EventArgs e)
{
//calculate

this.Field1 = "whatever";
}

GlennP
329

Look like a bit of fiddling and this might work! Thanks Foamy & Vulpes

GlennP
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

foamy
2499

Can you post the code that generates this warning?

GlennP
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

GlennP
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

foamy
2499

The code is difficult to read in a comment. Can you paste the entire method into a new Answer?

Feedback