Link to home
Start Free TrialLog in
Avatar of dvplayltd
dvplayltdFlag for Bulgaria

asked on

Call function in different child forms from MDI form with C#

Hi expert !

Im new in C# and need help. With NET Framework 2.0 and C# 2008 i need to make MDIform and many child forms with different textboxes and buttons and so on. I need to make buttons in MDI form and when click on this buttons to call function in child forms. If child form have not this function in it will not do nothing.

 I try
                 this.ActivateMdiChild(activeForm);
                 activeForm.aaaa();

but when compile have error which tell that function aaaa() doesnt exsist. How to avoid this befaour? May be with Reflection ?

Note: till now I reach to follow code which work

foreach (Form frm  in this.MdiChildren)
              if (frm is frmRequestAd)
              {

                  frmRequestAd f = frm as frmRequestAd;  f.aaaa(); break;

              }
}

, but I will have many chid form and many buttons and when add new child form I will need on EVERY button to add such text &
SOLUTION
Avatar of mirzas
mirzas
Flag of Bosnia and Herzegovina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of openshac
openshac

Create an event handler on the child form and hook it up to a method on the main form
    //in your child form set up a public event hander
 
    public event EventHandler<EventArgs> ChildDoStuff;
    protected virtual void OnChildDoStuff(EventArgs e)
    {
        if (ChildDoStuff != null)
            ChildDoStuff(this, e);
    }
 
 
    // in you main form hook up this event handler to a function called DoStuff()
    childform.OptimiserCancelled += new EventHandler<EventArgs>(DoStuffOnMainForm);
 
    private void DoStuff(object sender, EventArgs e)
    {
     ...
    }
            

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial