Solved

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

Posted on 2008-10-16
3
2,605 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 86

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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