I have two forms. One is MDI parent and the other is the MDI child. When a button on the parent is clicked the form of the child opens. I have written a code to prevent the form from duplicating. Those methods are as;
public Form IsFormAlreadyOpen(Type FormType)
{
foreach (Form OpenForm in Application.OpenForms)
{
if (OpenForm.GetType() == FormType)
return OpenForm;
}
return null;
}
public static Form1 f1;
public void open_Form1()
{
Form UForm = null;
UForm = IsFormAlreadyOpen(typeof(Form1));
if (UForm == null)
{
UForm = new Form1();
UForm.MdiParent = this;
UForm.Show();
}
}
This piece of code is available in the MDI Parent form. and them the button is clicked the method "open_Form1()" is called and it open the child form.
But the problem is how can I access the public method which is written in the child form? On the child form i have this code.
public void somefunction()
{
/*code*/
}
How can i call this method from the MDI Parent?
Once you've gotten a reference to a child form you need to cast it to the appropriate type then call the method. For example:
class MyChildForm : Form {}
Form childForm = GetAChildFormInstance();
MyChildForm castForm = (MyChildForm)childForm;
castForm.MyMemberMethod();
As a side note, you don't seem to have a naming convention. In .NET all public members (properties, fields and members) should be PascalCased. Locals (method variables and parameters) should be camelCase, and private fields can be _underscorePrefixed. Underscores should never be used in-between words in .NET.