1
votes

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?

2
What is the type of the Mdi forms?JaredPar
What do you mean with 'type'? public partial class EmpresasView : Form, IEmpresas ??Jorge Zapata
That's what I was looking for. I'll update my answerJaredPar

2 Answers

2
votes

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();
}
2
votes

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();
  }
}