Dev Center > Windows Forms Forums > Windows Forms General > How to access methods of MDI Child from by MDI Parent Toolbar?

Unanswered How to access methods of MDI Child from by MDI Parent Toolbar?

  • Friday, July 08, 2005 9:43 AM
     
     
    Hi

    How can I access the methods (Add, Save,Delete, Record navigation methods such first, previous, next & last) of a MDI child from 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. I am running there are several types of MDI child forms with keeping recordsets, but each contains (the same) methods...

    How can I write template class for this functions?

    Does anybody has an example of how to do this in Visual C++ .NET?

    Thanks,
    Joseph TA
    Tel: 0024398356388

All Replies

  • Sunday, July 10, 2005 10:13 PM
     
     

    If they all have the same methods, I think the easiest thing to do is to give all your MDI children the same base class, then override.  Alternately, you could create an interface they all implement.

    Here's how:
    Create a base form which has the methods, override the methods in your child forms, then from the MDIParent use the Form.ActiveMdiChild to obtain the currently active MDI child - cast to your base class, then call the method on it.

    Here's pseudocode C# for it:

    class MdiChildBaseForm : Form {
      protected virtual void MoveFirst() {
      }
    }

    class MdiChildForm1 : MdiChildForm {
       protected override void MoveFirst() { ... }
    }

    class MainForm : Form {
      private void  moveFirstToolStripButton_Clicked (object sender, EventArgs e) {
          MdiChildBaseForm f = this.ActiveMdiChild as MdiChildBaseForm;
          if (f != null) {
            f.MoveFirst();
         }
      }
    }

    Jessica

  • Monday, July 11, 2005 3:24 AM
     
     
    What's the best way to have the MDI Child access the toolbar on the parent form?  For example, if some of the buttons need to be disabled depending on the state of the child?
  • Monday, July 11, 2005 6:57 AM
     
     
    From the parent, you can use the MDIChildActivate event to detect when a new MDI child has become active, then refresh your button states based on the ActiveMdiChild.
  • Monday, July 11, 2005 8:57 AM
     
     
    Thanks for the reply. I hope this may give good result. But you written in second MdiChildForm is the parent class. Is correct or MdiChildBaseForm?

    Thanks
    Joseph TA
  • Monday, July 11, 2005 8:47 PM
     
     
                                           Form
             MdiChildBaseForm                     MainForm
    MdiChildForm1 MdiChildForm2

    Where
       MainForm 
          is the MdiParent with the ToolBar
       MdiChildBaseForm
          
    is the base class for all your MDIChildren in the project.  
         It has virtual methods for MoveFirst, MoveLast, etc etc
       MdiChildForm1 - is the first child form with an implementation for MoveFirst 
          It would override MdiChildBaseForm's MoveFirst and control its own DataGrid etc.
       MdiChildForm2 - is the second child form with an implementation for MoveFirst 
          Similar to MdiChildForm1, it would override MdiChildBaseForm's MoveFirst and control its own DataGrid etc.
  • Tuesday, July 12, 2005 8:41 AM
     
     
    Thanks for the help. Realy its very useful and solved my problem through these logic. First I written one abstract base class with pure virtual function and inherated for all the childforms.

    like this.

    class MDIChildBaseForm : Form {
    protected:
      virtual void NextRecord()=0;
      ...
    }

    class CustomerForm : MDIChildBaseForm {
    private:
      virtual void NextRecord(){...}
    }

    class ProductForm : MDIChildBaseForm {
    private:
      virtual void NextRecord(){...}
    }

    I hope it will work for my future software changes also.

    Thanks
    Jose
    Tel: 0024398356388/00243817300066
  • Tuesday, July 12, 2005 11:56 AM
     
     

     

    Hi

    Could please find why this problem occuring... while loading the Child Form in the design mode.


    I couldn't find the problem why its happening. But when I changing the base class (MDIChildBaseForm) of child form to

    System::Windows::Forms::Form its working fine. What problem with base class?

     

    --------------------------
    My MDIChildBaseForm code:
    --------------------------

    #pragma once

    #include "stdafx.h"


    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;


    namespace SCS
    {
     public __gc class MDIChildBaseForm : public System::Windows::Forms::Form
     {
     public:
      MDIChildBaseForm(void)
      {
       InitializeComponent();
      }
           
     protected:
      void Dispose(Boolean disposing)
      {
       if (disposing && components)
       {
        components->Dispose();
       }
       __super::Dispose(disposing);
      }
     public:
      virtual void FirstRecord()=0;
      virtual void PreviousRecord()=0;
      virtual void NextRecord()=0;
      virtual void LastRecord()=0;
     private:
      System::ComponentModel::Container* components;
      void InitializeComponent(void)
      {
       this->components = new System::ComponentModel::Container();
       this->Size = System::Drawing::Size(300,300);
       this->Text = S"MDIChildBaseForm";
      }  
     };
    }


    -----------------------------
    My Inherited Child Form Code:
    -----------------------------

    #pragma once

    #include "stdafx.h"
    #include "MDIChildBaseForm.h"

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;


    namespace SCS
    {
      public __gc class frmCurrency : public SCS::MDIChildBaseForm
      {
      public:
        frmCurrency(void)
        {
          InitializeComponent();
        }
           
        protected:
          void Dispose(Boolean disposing)
          {
     if (disposing && components)
     {
       components->Dispose();
     }
     __super::Dispose(disposing);
          }
        private:
          System::Windows::Forms::Label *  label1;
          System::Windows::Forms::TextBox *  txtCurrencyID;
          System::Windows::Forms::Label *  label2;
          System::Windows::Forms::TextBox *  txtCurrencyName;
          System::ComponentModel::Container* components;

        void InitializeComponent(void)
        {
          this->label1 = new System::Windows::Forms::Label();
          this->txtCurrencyID = new System::Windows::Forms::TextBox();
          this->label2 = new System::Windows::Forms::Label();
          this->txtCurrencyName = new System::Windows::Forms::TextBox();
          this->SuspendLayout();
          //
          // label1
          //
          this->label1->Location = System::Drawing::Point(96, 64);
          this->label1->Name = S"label1";
          this->label1->TabIndex = 0;
          this->label1->Text = S"&Currency ID:";
          this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
          //
          // txtCurrencyID
          //
          this->txtCurrencyID->Location = System::Drawing::Point(200, 64);
          this->txtCurrencyID->Name = S"txtCurrencyID";
          this->txtCurrencyID->TabIndex = 1;
          this->txtCurrencyID->Text = S"txtCurrencyID";
          //
          // label2
          //
          this->label2->Location = System::Drawing::Point(96, 104);
          this->label2->Name = S"label2";
          this->label2->TabIndex = 2;
          this->label2->Text = S"Currency &Name:";
          this->label2->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
          //
          // txtCurrencyName
          //
          this->txtCurrencyName->Location = System::Drawing::Point(200, 104);
          this->txtCurrencyName->Name = S"txtCurrencyName";
          this->txtCurrencyName->TabIndex = 3;
          this->txtCurrencyName->Text = S"txtCurrencyName";
          //
          // frmCurrency
          //
          this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
          this->ClientSize = System::Drawing::Size(528, 374);
          this->Controls->Add(this->txtCurrencyName);
          this->Controls->Add(this->label2);
          this->Controls->Add(this->txtCurrencyID);
          this->Controls->Add(this->label1);
          this->Name = S"frmCurrency";
          this->Text = S"Currency";
          this->Load += new System::EventHandler(this, frmCurrency_Load);
          this->ResumeLayout(false);
        }  
      
        System::Void frmCurrency_Load(System::Object *  sender, System::EventArgs *  e)
        {
          ...
        }

        System::Void btnPrevious_Click(System::Object *  sender, System::EventArgs *  e)
        {
           PreviousRecord();
        }

        System::Void btnFirst_Click(System::Object *  sender, System::EventArgs *  e)
        {
           FirstRecord();
        }

        System::Void btnNext_Click(System::Object *  sender, System::EventArgs *  e)
        {
           NextRecord();
        }

        System::Void btnLast_Click(System::Object *  sender, System::EventArgs *  e)
        {
     LastRecord();
        }
      public:
        virtual System::Void FirstRecord()
        {
     ...
        }
        virtual System::Void LastRecord()
        {
     ...
        }
        virtual System::Void NextRecord()
        {
     ...
        }
        virtual System::Void PreviousRecord()
        {
     ...
        }
      };
    }


    -----------------------------------------------------------------------------
    While loading form in the Microsoft Visual Studio .NET IDE giving this error:
    -----------------------------------------------------------------------------

    An error occurred while loading the document. Fix the error, and then try loading the document again. The error message

    follows:

    The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected

    the following classes in the file:

    frmCurrency --- The base class 'SoficomCollectionSystem.MDIChildBaseForm' could not be loaded. Ensure the assembly has been

    referenced or built if it is part of the project.


    Thanks
    Jose


     

  • Tuesday, July 12, 2005 10:13 PM
     
     
    Did you ever have the frmCurrency class inheriting from the MDIChildBaseForm class and loading in the designer?  Or did you have this working and then made a change? 

    Can you get this to work in the simple case of creating a new project and adding two forms with the second inheriting from the first?

    thanks
     - mike
  • Wednesday, July 13, 2005 9:39 AM
     
     
    Hi

    I didn't try with new project so far. Before I want to know why this problem happening while designing my form so that I can fix and continue my work without work. Because I am going to develop lot of forms with that base class.

    Could you find the problem? How I can solve? Please who have knowledge in this area help me.

    Thanks in advance.
    Jose