Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Communication between MDI form 3

Status
Not open for further replies.

ajikoe

Programmer
Joined
Apr 16, 2004
Messages
71
Location
ID
Hello I would like to build MDI form,

Can anyone knows how to :
1. making global variable with can be exchanged between child and parent form.

2. make my child form be able to change parents variable or parents control property (for example parent form text variable).

Thanks.
pujo
 
This is a common question. Please do a search.

The short answer is: Forms are objects, and all object member visibility rules apply.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
As chiph said you can find may examples about MDI forms and I add only few comments.
You have two objects Obj1 and Obj2 and you want these objects to communicate.
What you need is Obj1 KNOWS Obj2 and Obj2 KNOWS Obj1.
One way to accomplish this relationship is to have in Obj1 a reference to Obj2 and in Obj2 a reference to Obj1.
Another way is to have an intermediate class which in fact encapsulates the realtionship between the two objects e.g. an object of this class KNOWS the both objects Obj1 and Obj2.
Whenever you need to refer the Obj2 from Obj1 , you should ask this intermediate object to supply a reference.
For the case you asked , a Form object already has a property named MdiParent in which you can store a reference to the parent form.
So, when you create a child form , you have to supply a reference to the parent form and store it in the MdiParent.
In the main form :
Code:
public MainForm() 
{

//
this.IsMdiContainer = true;
//...

this.MdiChildActivate += new EventHandler(this.MDIChildActivated);

// create another form and store the parent reference
Form2 frm2 = new Form2();
frm2.MdiParent = this;
//

}
-obislavu-
 
Sorry for not minding my own business! Just wanted to thank obislavu for the nice info!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Thanks Obislavu,

You said before:
"What you need is Obj1 KNOWS Obj2 and Obj2 KNOWS Obj1.
One way to accomplish this relationship is to have in Obj1 a reference to Obj2 and in Obj2 a reference to Obj1."

I wonder how can I make the reference of two form which doesn't have parent and child relationship

pujo
 
ajikoe -
You can relate any two objects this way -- they don't have to have a parent/child relationship.

So, as long as class1 has a member variable of type class2, once an instance of class1 is created, it has the potential of knowing about a class2 instance. Once the class2 member has been pointed at an instance of a class2, then class 1 is able to use all the public methods of that class2 instance.

You can think of it this way -- imagine your mobile phone didn't have a keypad, but instead only allowed you to dial via it's phonebook. Since you bought the cheap phone, it only has one phonebook entry -- which is currently empty. Since your phonebook is empty you can't talk to anyone (but others can still call you via your number). Once you've added someone's number to your phonebook, you can now call them via their public interface (their number) and talk to them. If you have an argument and you're no longer friends, you erase the phonebook entry for them (set it to null), and you can now store someone else's phone number there.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top