最好通过声明一个接口来实现:
public interface IChildCommands {
void Add();
void Save();
void Cancel();
}
并让MDI子窗体实现它:
public partial class Form2 : Form, IChildCommands {
// Right-click IChildCommands in the editor and choose Implement Interface
//...
}
在父级中,实现工具栏按钮的单击事件,如下所示:
private void AddButton_Click(object sender, EventArgs e) {
var child = this.ActiveMdiChild as IChildCommands;
if (child != null) child.Add();
}
void Application_Idle(object sender, EventArgs e) {
AddButton.Enabled = this.ActiveMdiChild is IChildCommands;
// etc..
}