-
forms!!!???
Hello:
If there is one form1 with menu items and one textbox on it, textbox is public type,I click the menu item,
another form2 with a button pops up, and how could I point the textbox on form1,
because if I create such
form1 myform1 = new form1();
form1.textbox.Text = "test";
on form2 button events
it didn't work, because this myform1 is another memory copy of form1, so, what should I then?
Thank you!!
-
Your form2 needs a variable of type form1 that has to be set to myform1 before shosing form2. Then you can do whatever you want on form1.
-
Thanks, but can you write a simple code to illustrate this, I got a little bit confused :(
-
and somehow, each form is a independ class, so I can't define such global form instance that can be used in both forms
-
I'm a bit too lazy to type code today...
In your form2 class, make a public variable of type form1 and call it parent.
If your form1 class, after creating the variable of type form2, set it's parent to this. Then in form2 you can access the form1 variable through the variable parent.
-
ok, so in form1:
i did like this:
Form2 form2 = new Form2();
form2.Parent = this;
then
form2.Show();
in form2;
public Form1 form1;
form1 = Parent; // but i get errors
by the way, is this working for MDI things?
thanx!
-
This is not what I said. If you want the above solution look again at what I wrote and follow it step by step.
However, Forms obviously already have a Parent property, you just need to use it. In your form2, you can access form1 by calling
((Form1)Parent).WhateverYouWantOfForm1.
Example:
In form2:
((Form1)Parent).Title = "Changing fom1's title";