How to access methods of MDI Child from by MDI Parent Toolbar?
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
[630 byte] By [
JoseTA] at [2007-12-16]
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
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
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