Reputation: 2336
I have a save button in MDI parent form and I want to call some method in the active MDI child form everytime the user click this button.
Suppose I have the activemdichild.name stored in a variable.
string name = this.ActiveMdiChild.Name.ToString();
And all my MDI child forms have a save method.
public void SaveForm()
{
//Some code here
}
How do I programmatically call SaveForm method?
If this aren´t best practices, what do you suggest?
Upvotes: 1
Views: 2333
Reputation: 754725
Assuming the type of the MDI Form children is MyMdiForm
you can do the following
foreach (var form in MdiChildren) {
var view = form as IEmpresas;
if (view != null) {
view.SaveForm();
}
}
Upvotes: 2
Reputation: 3694
What about having your child forms implement an interface that defines the kinds of things you expect your mdi children to implement.
Such as:
IChildWindow
{
void Save()
}
public class MyChildClass : IChildWindow
{
public void Save()
{
}
}
Then in your mdi parent form:
foreach (var child in MdiChildren)
{
var childAsIWindow = child as IChildWindow;
if (childAsIWindow == null) throw new InvalidOperationException("Not a IChildWindow");
// or you could just ignore them.
childAsIWindow.Save();
}
Upvotes: 2