How do I load a new form and access varables from it?
-
I want to bring up a second form to adjust variables on the first form. Any help appreciated! -mutty
First Is This Variable Public if Yes You Can Do Somthing Like That in Form1
Form2 form2=new Form2(); form2.variableName=value;
If this variable is not public which will be better You can Write public function in the form and make it set the value of the variable you can code somthing like thar in Form2 which have the variable you wwant to access
public voide SetValue(int i) { myVariable=i; }
and in Form1 You can Write
Form2 form=new Form2(); form2.SetValue(1);
MCAD
-
First Is This Variable Public if Yes You Can Do Somthing Like That in Form1
Form2 form2=new Form2(); form2.variableName=value;
If this variable is not public which will be better You can Write public function in the form and make it set the value of the variable you can code somthing like thar in Form2 which have the variable you wwant to access
public voide SetValue(int i) { myVariable=i; }
and in Form1 You can Write
Form2 form=new Form2(); form2.SetValue(1);
MCAD
-
Sorry, I should have been clearer what I mean. I am trying to open a new form, (Form2), from my main form (form1) On form2 are numerous controls which should change variables of form1.
Pass a reference to form1 in form2's constructor and store it inside form2: in form 1: Form2 form2 = new Form2(this); form2.Show(); in form 2: public Form2(Form1 refToForm) { this.form1 = refToForm; } private someMethod() { refToForm.SomeProperty = someValue; }
-
Sorry, I should have been clearer what I mean. I am trying to open a new form, (Form2), from my main form (form1) On form2 are numerous controls which should change variables of form1.
Mutty wrote: am trying to open a new form, (Form2), from my main form (form1) In Form1
Form2 form2=new Form2(); form2.Show(); Mutty wrote: On form2 are numerous controls which should change variables of form1 in form2 like in my other message Form1 form1=new Form1(); form1.var=value;
MCAD