?
Solved

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

Posted on 2008-10-16
3
Medium Priority
?
2,608 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Assisted Solution

by:mirzas
mirzas earned 600 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 86

Accepted Solution

by:
Mike Tomlinson earned 900 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

765 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