Solved

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

Posted on 2008-10-16
3
2,603 Views
Last Modified: 2013-12-17
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 &
0
Comment
Question by:dvplayltd
3 Comments
 
LVL 6

Assisted Solution

by:mirzas
mirzas earned 200 total points
ID: 22729746
Create a base form class which contains the methods. All of the child forms should inherit this class and/or override the methods from the base class.
0
 
LVL 6

Expert Comment

by:openshac
ID: 22729768
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

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 22732236
Instead of Inheritance, make all of your MdiChild Forms Implement an INTERFACE:
http://msdn.microsoft.com/en-us/library/87d83y5b(VS.80).aspx

Then you can iterate the MdiChildren() collection, or use the ActiveMdiChild() property, and cast the form(s) to your Interface type and call the methods that you have defined in the Interface.

It is possible to do this with Reflection as well but it is not as elegant.  You would basically need to catch the exceptions generated with Try/Catch blocks and/or check for null returns indicating that the method/control you are looking for didn't exist.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question