tags:

views:

22

answers:

1

Hello, We are developing an MDI Application. What we need to do is that I Have 4 child forms and 1 Parent form. on the parent form ToolBox menu I have 3 buttons. Add,Save,Cancel.

What I want to do is What ever the child form has loaded. When these buttons clicked they should process the action on the child form.

LEts say, If my child form named CustomerManager is open, then by pressing the add button it should basically process my CustomerManager child form. Obviously I will right the logic against every form action Button.

I hope I am able define what I am looking for.

Regards Shax

A: 

This is best done by declaring an interface:

public interface IChildCommands {
    void Add();
    void Save();
    void Cancel();
}

And let your MDI child form implement it:

public partial class Form2 : Form, IChildCommands {
   // Right-click IChildCommands in the editor and choose Implement Interface
   //...
}

In your parent, implement the Click event for the toolbar button like this:

    private void AddButton_Click(object sender, EventArgs e) {
        var child = this.ActiveMdiChild as IChildCommands;
        if (child != null) child.Add();
    }

It is also a good idea to disable the button if a child is active that doesn't implement the command. Which you can do by writing an event handler for the Application.Idle event:

    void Application_Idle(object sender, EventArgs e) {
        AddButton.Enabled = this.ActiveMdiChild is IChildCommands;
        // etc..
    }
Hans Passant
Thanks Hans, I will do as per your suggestion and will update you if found problem....
Shax