Friday, February 15, 2008

How to access Parent Form Methods from Child Form in .NET C#?



/* How to access Parent Form Methods from Child Form in .NET C#?

There may be 101 ways.

Simplest way I know is to create a Public Static variable to Carry your Parent form. :)

Here it is. */

//Add this code in the parent form


//declare the public static variable.
public static Parent ParentInstance;


//before you call the ChildInstance.Show(), assign the parent form to ur variable.

ParentInstance = this;
ChildInstance.Show();


//Add this code piece in the Child form

Parent.ParentInstance.YourMethodName();

/* Beauty is you don't need to declare your method as Static, which is not possible in most of the occasions [when u access form controls in ur method]*/