• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 609
  • Last Modified:

How to call an MDIChild public function from the MDIParent - VS2008

Hello Again...
I'm sure you've heard of this one before also.  I'm creating public Save() functions on each of my MDIChild forms, but I can not call those functions from the MDIParent.  As a matter of fact, I can not even compile the application, I get an error.  So, my question is, how do I call a public function from my MDIParent form for the multiple different MDIChild forms?
0
ljmilano
Asked:
ljmilano
  • 4
  • 2
1 Solution
 
RPCITCommented:
           foreach (Form childForm in MdiChildren)
            {
                childForm.Save();
            }
0
 
ljmilanoAuthor Commented:
Hi, can you give me a little more details/explanation?  Also, I only want to call the Save() one form at a time...this would do them all right?  Thanks.
0
 
RPCITCommented:
Yes... this would run it against all of them...

How will you know which form you want to save?  by Class Name?

The issue you may be missing is that you can launch the same for many time in a MDI environment, so do you want to save all the instances of a certain "type" of form?  or a specific one?  If so.. why are you running it from the MDI parent?

you could get a sindle instance of the form by doing an if inside the foreach()..

foreach (Form childForm in MdiChildren)
{
   if(childForm is FormClassName)
   {
      childForm.Save();
   }
}
 
//or...
foreach (Form childForm in MdiChildren)
{
   if(childForm.Text == "form caption")
   {
      childForm.Save();
   }
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ljmilanoAuthor Commented:
Okay, let me try your suggestions...I'll do some testing and let you know.
0
 
ljmilanoAuthor Commented:
I tried this...and it won't compile, the error I get on the "ChildForm.save() line is:
 'save' is not a member of 'System.Windows.Forms.Form'
                For Each ChildForm As Form In Me.MdiChildren
                    If ChildForm.Name = "frmImport" Then
                        ChildForm.save()
                    End If
                Next
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use Reflection to execute a method "by name" against each MdiChild:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each frm As Form In Me.MdiChildren
            Dim mi As System.Reflection.MethodInfo = frm.GetType.GetMethod("Save", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic)
            If Not IsNothing(mi) Then
                mi.Invoke(frm, Nothing)
            End If
        Next
    End Sub

If you only want it to execute for a certain type of form then use something like:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each frm As Form In Me.MdiChildren
            If TypeOf frm Is frmImport Then
                Dim mi As System.Reflection.MethodInfo = frm.GetType.GetMethod("Save", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic)
                If Not IsNothing(mi) Then
                    mi.Invoke(frm, Nothing)
                End If
            End If
        Next
    End Sub

0
 
ljmilanoAuthor Commented:
Wow, pretty awesome, I'm going to have to understand the "System.Reflection" a little better.  Thanks for your help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now