Solved

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

Posted on 2008-10-16
3
2,593 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now