Can MDI child call back parent variable or any object?
e.g.
- frmParent
-Public String conn = "abc.mdb";
- frmChild
- Public String test = "demo";
- frmChildTest
- how can we frmParent.conn ?
- and can we call frmChild.test , too ?
the issue i would like to call parent variable,
It would like to create the database connection in parent ,
so each child only need to call parent connection, and dont need to recreate on each form
Every MDI child has a reference to its parent in its MdiParent property, which YOU would have explicitly set when you created it. The reference is type Form so you will need to cast it as the appropriate type to access members of that type, e.g.
Code:
MessageBox.Show(((frmParent)this.MdiParent).conn)
Having said that, you should be using application settings for this type of thing in C# 2.0. You go to the Settings tab of the project properties and create a setting, named ConnectionString for example, of type string with Application scope. Now you can access that value ANYWHERE in your app, e.g.
There is even a specialised type of setting for connection strings, which you can select from the drop-down list in the Type column instead of string. It will then allow you to build your connection string visually in the Value column instead of having to type it character by character. You can also bind properties of components created in the designer to application settings, so you could create your connection in the designer for each child form and bind its ConnectionString property to this setting, so it gets loaded automatically every time.
Last edited by jmcilhinney; May 30th, 2006 at 05:57 PM.