Shaitan00 Posted December 30, 2003 Posted December 30, 2003 The user starts on form1 [this.] and at an event [click] he is brought to a new form [form2], during this call parameters are also passed into the new form. The user then performs some actions in form2 and returns to form1. My problem is that I need a string value to be passed from form2 to form1 when the user returns. : FORM1: this.Hide(); System.Windows.Forms.DialogResult dr = (new Form2(Settings, Registry)).ShowDialog(this); this.Show(); FORM2: string Name = �bla�; // Not Actual Value this.DialogResult = System.Windows.Forms.DialogResult.OK; this.Close(); So as described when the user loads Form2 variables "Setting" and "Registry" are passed in, how could I somehow pass the variable �Name� back to Form1? Is this even possible? Same we mimic the method used to pass variables into from2 [from form1]? Quote
*Experts* mutant Posted December 30, 2003 *Experts* Posted December 30, 2003 If you are passing in the instance in the ShowDialog method then you can later use th Owner property from your second form. SO before you close form2 do this for example: Form1 parent = (Form1)this.Owner; //set a stringv alue to something parent.hello = "Hello"; Quote
Shaitan00 Posted December 30, 2003 Author Posted December 30, 2003 Not sure I understand what you mean. I assume you want your bit of code to be place here: string Name = x; // Where x is a value compiled in Form2 this.DialogResult = System.Windows.Forms.DialogResult.OK; Form1 parent = (Form1)this.Owner; // Compiles no Problem parent.hello = Name; this.Close(); The parent.hello = Name; is what I do not understand, would this require some kind of definition of HELLO in Form1? [because for fun running your exact code causes the following error: Project.Form1' does not contain a definition for 'hello'] So I assume I need to add some kind of definition of variable "hello", can this be done in my Form1 code? [define a string hello = null;] ??? Quote
*Experts* mutant Posted December 30, 2003 *Experts* Posted December 30, 2003 Hello was used as just an example :). Replace hello with the name of your string. :) Quote
Shaitan00 Posted December 30, 2003 Author Posted December 30, 2003 I see that "Hello" was an example, however replacing it with anything causes the exact same error [except with respect to whatever you changed it to]. So you used .Hello = Name let's say for me it would be .FName = Name [where FName is the variable stored in Form1 to house this result] However doing this generates the same error: Project.Form1' does not contain a definition for 'FName'] So I assume that somewhere in Form1 I need to define variable FName. can this be done in Scope of the code I posted below or must I create a new private string member to my Form1 class? Quote
*Experts* mutant Posted December 30, 2003 *Experts* Posted December 30, 2003 If you want to pass a string back to the form1, you need to have some variable that will hold that string. So yes, you must create a new variable in your Form1, FName like you said. But do not make it private so you can access it from Form2 (unless you want to make use of properties), and also do not declare it within any method, just declare it so its availabe to the other form. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.