[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2008-10-16
3
Medium Priority
?
2,612 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 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