none
How to access methods of MDI Child form by MDI Parent toolbar?

    Question

  • How can I access the methods (save, new, delete methods) of a MDI child form when clicking on a toolbar positioned in the MDI parent form?

    So on the MDI parent I have this toolbar. When clicking on a button on this toolbar it must call a method from the current MDI child form. There are several types of MDI child forms, but each contains (the same) methods...

    Does anybody has an example of how to do this?

    Thanks,

    Alwin
    Monday, July 04, 2005 1:27 PM

Answers

  • If you haven't already, you need to look at creating an Interface that specifies everything that is common to each MDI Child Form in your application (Save, New, Delete, etc Methods).  Have each form implement this interface so they have a common contract.  Then from your toolbar you can access it's parent, which I believe should be the MDI Form, get it's active child and cast it as the interface you created and call whatever method you want.  It might look something like this (not sure which language you're doing this in):



    Public Interface IChildForm
       Sub Save()
       Sub [New]()
       Sub Delete()
    End Interface

    DirectCast(DirectCast(MyToolBar.Parent, Form).ActiveMdiChild, IChildForm).Save()

     
    Monday, July 04, 2005 5:56 PM
  • Hi,


    Yup, its possible. Erik Porter's suggestion is very good. To make it simplier, try creating a public method in your childform:

          public sub EnableButtons()
             btnSave.Enable = false;
             '' Disable whatever controls you want to disable in the child form
          End Sub

    And in your MDI:

          private sub button_click(sender as object, e as eventargs)
             Dim childForm As <ChildFormName> = DirectCast(<ChildFormName>, Me.ActiveMdiChild)
             childForm.EnableButtons();
          end sub




    cheers,


    Paul June A. Domag
    Wednesday, July 06, 2005 5:33 AM

All replies

  • If you haven't already, you need to look at creating an Interface that specifies everything that is common to each MDI Child Form in your application (Save, New, Delete, etc Methods).  Have each form implement this interface so they have a common contract.  Then from your toolbar you can access it's parent, which I believe should be the MDI Form, get it's active child and cast it as the interface you created and call whatever method you want.  It might look something like this (not sure which language you're doing this in):



    Public Interface IChildForm
       Sub Save()
       Sub [New]()
       Sub Delete()
    End Interface

    DirectCast(DirectCast(MyToolBar.Parent, Form).ActiveMdiChild, IChildForm).Save()

     
    Monday, July 04, 2005 5:56 PM
  • thanks for the info... But can I also disable certain buttons (like Save) on the toolbar of the main MDI form?

    Does anybody have an example of a program with MDI child and toolbars located on the MDI form?

    Thanks...

    ps. (I'm doing it in VB.net)

    Wednesday, July 06, 2005 3:09 AM
  • Hi,


    Yup, its possible. Erik Porter's suggestion is very good. To make it simplier, try creating a public method in your childform:

          public sub EnableButtons()
             btnSave.Enable = false;
             '' Disable whatever controls you want to disable in the child form
          End Sub

    And in your MDI:

          private sub button_click(sender as object, e as eventargs)
             Dim childForm As <ChildFormName> = DirectCast(<ChildFormName>, Me.ActiveMdiChild)
             childForm.EnableButtons();
          end sub




    cheers,


    Paul June A. Domag
    Wednesday, July 06, 2005 5:33 AM
  • Hi Alwin,

        -> Create a common Interface ex:- IChildForm with the method save, 
             delete  etc.,

        -> Inherit this interface in all ChildForms and implement those methods
        
        -> Have a common static variable for the Interface

            ex:- public static IChildForm ChildFormInterface;

        -> Assign the child form to the static variable when it is showed
            
            ex:-
                 Form2 frm = new Form2();
                 ChildFormInterface = frm;
                 frm.Show();
         
         -> Now you can call the methods using this interface. For example
             just the add following code to the button save             

                      ChildFormInterface.Save();
         
    I hope the above example will help u.

    Thursday, July 21, 2005 9:46 AM
  • Hello.
    Your post was very helpfull.(sorry for my English if i make eny errors).

    I use MS VS NET 2003 C++.
    I have parent form "Form1", mdichildform "mdidziecko" and Interface "Iform".
    An interface:

    #pragma once

    public __gc __interface Iform{

    void napisz();

    };


    declaration of static variable for interface :

    public: static Iform * ala;


    Code making mdichild:

    mdidziecko * dziecko =
    new mdidziecko;

    ala = dziecko;

    dziecko->MdiParent = this;

    dziecko->Show();

    And when I use: ala->napisz();

    It works, but for last created mdichild. But I have more than one mdichild, and I want that the ala-> napisz() will work for the ActiveMdiChild in this moment.

    In this topic is posted this, what I want, but in VisualBasic(I think DirectCast...).

    Can you write for me this in c++ ?

    Thanks for all :)

    Wednesday, December 07, 2005 10:35 AM
  • I'm answering for my own question :)

    I found :)

    use :

    ala = dynamic_cast<Iform*> (ActiveMdiChild);

    ala->napisz();

    for example :P

    Thursday, December 08, 2005 12:54 PM