ChildForm need to access functions on ParentForm ?

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

ChildForm need to access functions on ParentForm ?

Postby Ruanova » Tue Apr 26, 2005 6:23 am

Hi, I'm have a Main Form that creates a serie of child forms ... by
Tchild *frm = new Tchild(this)


But, in some cases, the child need to call some functions and read/write some properties on then Parent Form ...

Now I'm use a callback function, but is very hard create a callback function for any function on Parent Form.
If I add an include to MainForm in then Child Form ... have errors in then compile progress ...

Can help me with any idea ...

Thanks in advance.
Ruanova
 

Postby Damon » Wed Apr 27, 2005 7:07 am

Hi Ruanova,

Trditionally, this is done by including the header file of the MDI parent window in the .cpp file of the MDI child window...

Code: Select all
// in ChildWin.cpp




And, the MDI parent would include the child window's header file in either its own header file or its .cpp file...

Code: Select all
// in Main.h or Main.cpp



Try this, and see if you still have any error messages.

Good luck,
Damon
Damon Chandler
Editor-in-Chief
C++Builder Developer's Journal
http://bcbjournal.com
User avatar
Damon
BCBJ Editor and Admin
BCBJ Editor and Admin
 
Posts: 285
Joined: Wed May 26, 2004 11:25 pm
Location: Stillwater, OK, USA

Postby Ruanova » Fri Apr 29, 2005 1:24 am

Ok, thanks Damon ...
I know this ... but see this code:

Chid.cpp:

Code: Select all






Child.h

Code: Select all
  FrmMain* myParentForm;




Main.cpp:

Code: Select all
 #include "child.h"
 #include "main.h"




Main.h:

Code: Select all
private:     
     frmchild *mychild;

     // THIS LINE CAUSE AN ERROR:
     //[C++ Error] uFrmMain.h(127): E2238 Multiple declaration for 'TFrmMain::TFrmChild'




The solution I found is change the declaration on CHILD form like this:

Chid.cpp:

Code: Select all


.
.
.
TFrmChild *FrmChild;

//HERE
TFrmMain *myParentForm;

//---------------------------------------------------------------------------
__fastcall TFrmChild::TFrmChild(TComponent* Owner): TForm(Owner)
{
}





Child.h
(none)


But this make it PRIVATE and inaccesible by the MainForm....

----------------
Sorry for my 'horrible' post.....
Ruanova
 

Postby Damon » Tue May 03, 2005 9:30 am

Ruanova,

There's no need to carry around global pointers to the forms (e.g., TFrmChild *FrmChild; TFrmMain *myParentForm; in your code). The children can access the parent form by using...

Code: Select all
TFrmMain* ParentForm = static_cast<TFrmMain*>(Application->MainForm);


And the parent form can access the child forms by using the TForm::MDIChildren[] property (casted to TFrmChild).

Good luck,
Damon
Damon Chandler
Editor-in-Chief
C++Builder Developer's Journal
http://bcbjournal.com
User avatar
Damon
BCBJ Editor and Admin
BCBJ Editor and Admin
 
Posts: 285
Joined: Wed May 26, 2004 11:25 pm
Location: Stillwater, OK, USA


Return to Technical

Who is online

Users browsing this forum: No registered users and 21 guests

cron