开发者

MDI Application Main Menu help

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 me开发者_C百科nu 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


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..
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜