Reputation: 11
I have a main (MDI) form and around 70 child forms which are intialized and shown in runtime. At a time maybe more than 1 child form can be shown. In each of the child forms there are button named "OK" and its event "btnOk_click(object sender, EventArgs e). There is a button in the Parent form named "Save", if we click on it, in the runtime the activeMDI child forms event (btnOk_click) should get fired.
Please help me on this issue.
at present I do this issue, by using following code
switch (ActiveMdiChild.GetType().Name)
{
case "frmSalesOrder":
case "frmPurchaseOrder":
case "frmSizeRatio":
break;
case "frmUserGroup":
var _frmUserGroup = (frmUserGroup)this.ActiveMdiChild;
_frmUserGroup.btnOK.PerformClick();
_frmUserGroup = null;
break;
case "frmUser":
var _frmUser = (frmUser)this.ActiveMdiChild;
_frmUser.btnOK.PerformClick();
_frmUser = null;
break;
Thanks Joseph J
Upvotes: 1
Views: 2478
Reputation: 81675
Interfaces could really help you here:
interface IChildSave {
void SaveAction();
}
Then in each of your child forms, implement it:
public partial class Form1 : Form, IChildSave {
public void SaveAction() {
saveButton.PerformClick();
}
private void saveButton_Click(object sender, EventArgs e) {
// save routine
}
}
Then your MDI Parent form wouldn't need the Switch statement any longer:
if (this.ActiveMdiChild is IChildSave) {
((IChildSave)this.ActiveMdiChild).SaveAction();
} else {
MessageBox.Show("Child Form does not implement IChildSave.");
}
Upvotes: 8
Reputation: 2138
There are a lot of ways of doing this. Yours is one of them (but very hard to follow with 60 forms!)..
Anyway you can make every child to subscribe to an event that it raise when you press save.
Upvotes: 0