Board index » cppbuilder » Need Help Accessing Dynamic Form

Need Help Accessing Dynamic Form


2003-07-21 04:06:00 AM
cppbuilder107
I created a form using the following method:
TFormName* dlg = new TFormName(this);
dlg->ShowModal();
delete dlg;
I then have a button on that form that creates another form; when the user
click the
ok button on the second form I need to modify some control on the first
form. But
since the form was created dynamically I have no way to do this.
I was thinking that I have to use the 'dynamic_cast' but I am still not sure
how this works, so I need some help.
Thanks!
 
 

Re:Need Help Accessing Dynamic Form

"oLiVeS" < XXXX@XXXXX.COM >writes:
Quote
I created a form using the following method:

TFormName* dlg = new TFormName(this);
dlg->ShowModal();
delete dlg;
If an exception escapes from ShowModel, then your form will fail to be
deleted. You can fix this problem by changing your code to this:
#include <memory>
...
std::auto_ptr<TFormName>dlg(new TFormName(this));
dlg->ShowModal();
It behaves exactly the same as what you wrote, but involves LESS
typing, and will properly delete the form even if an exception occurs.
Nice idiom to remember... (auto_ptr automatically deletes a pointer
when it goes out of scope, but tries to act like a normal pointer in
other ways. They do become tricky when you have to copy them around,
but in the simple case like you're using the pointer, auto_ptr is the
"perfect" solution. Or, boost::scoped_ptr, if you're using the boost
library (which doesn't come with BCB but is freely available from the
web. auto_ptr is part of the C++ language, though.)
Quote
I then have a button on that form that creates another form; when
the user click the ok button on the second form I need to modify
some control on the first form. But since the form was created
dynamically I have no way to do this.
When you create the 2nd form, it can store a pointer to the dlg form.
Thus, using that pointer, you can call functions on the form that
created it by using that pointer.
Quote
I was thinking that I have to use the 'dynamic_cast' but I am still
not sure how this works, so I need some help.
I'm not sure of the details in VCL code, but if you have a base
pointer and need to "downcast" it to a derived type, AND you are not
sure that the cast is safe (correct), then use dynamic_cast. It does
some runtime type checking to ensure that downcasting is a valid
operation, and returns a pointer to the derived object upon success,
or a null pointer if the cast fails.
(dynamic_cast on references returns a reference to the derived type,
or throws a std::bad_cast exception upon failure. But you're not
casting references, so this isn't really important to you right now.)
--
Chris(TeamB);
 

Re:Need Help Accessing Dynamic Form

Quote
I have been able to create my forms using the new method as show here:
std::auto_ptr<TFormName>dlg(new TFormName(this));
dlg->ShowModal();
Quote
But I am still having the same problem, I don't know how to access the
parent form.
Form1 (Main Form)
Form2 (Dynamic Form 1)
Form3 (Dynamic Form 2)
Quote
In this example I need Form3 to access Form 2, How?
Thanks!
 

{smallsort}

Re:Need Help Accessing Dynamic Form

"oLiVeS" < XXXX@XXXXX.COM >writes:
Quote
Form1 (Main Form)
Form2 (Dynamic Form 1)
Form3 (Dynamic Form 2)

>In this example I need Form3 to access Form 2, How?
Make Form2's constructor take a pointer to Form1, and change Form2's
constructor to store that pointer as member data. Then when Form2
needs to talk to Form1, it just calls functions on that pointer.
--
Chris(TeamB);
 

Re:Need Help Accessing Dynamic Form

An example would help a lot if you have one up your sleeve.
I understand what you are saying, but I just not 100% sure how to do that.
Here is what I got and you can correct me if I am wrong.
----------------------------------------------------------------------------
---
__fastcall TEditMessage::TEditMessage(TComponent* Owner)
: TForm(Owner)
{
// Create pointer to the 'EditProfile' dialog
/* The problem with is that I don't now how to create a pointer to a
form.
I only know how to create a new form and that is not what I want. */
std::auto_ptr<TEditProfile>EditProfile_ptr(TEditProfile(this)); //
Compiler Error
}
----------------------------------------------------------------------------
---
void __fastcall TEditMessage::OkClick(TObject *Sender)
{
// This modifies a checkbox on another form which created this form.
EditProfile_ptr->ShowMessage->Enabled = true; // Compiler Error
EditProfile_ptr->ShowMessage->Checked = true; // Dido
}
----------------------------------------------------------------------------
---
Quote
Make Form2's constructor take a pointer to Form1, and change Form2's
constructor to store that pointer as member data. Then when Form2
needs to talk to Form1, it just calls functions on that pointer.
Chris(TeamB);
 

Re:Need Help Accessing Dynamic Form

I am assuming this part is being defined in the header
Quote
You can do something like the following:

class TEditMessage : public TForm
{
private:
TEditProfile *EditProfile;
public:
__fastcall TEditMessage(TEditProfile *AProfile);
};
This part kind of threw me off; is this in the header too, or in the source
file?
Quote
__fastcall TEditMessage::TEditMessage(TEditProfile *AProfile)
: TForm(Application), EditProfile(AProfile)
{
}
 

Re:Need Help Accessing Dynamic Form

"oLiVeS" < XXXX@XXXXX.COM >wrote in message
Quote
I am assuming this part is being defined in the header
The class declaration goes in a header file. The class body definitions go
in a source file.
Gambit
 

Re:Need Help Accessing Dynamic Form

I am still having trouble; I am not sure if I have it the right places, so
here is what I got.
---------------------------------------------------------------------------
Header for EditMessage
---------------------------------------------------------------------------
class TEditMessage : public TForm
{
__published: // IDE-managed Components
TMemo *Memo;
TButton *Ok;
TButton *Cancel;
void __fastcall MemoChange(TObject *Sender);
void __fastcall OkClick(TObject *Sender);
private: // User declarations
TEditProfile *EditProfile; // Added by user;
compiler error!
public: // User declarations
__fastcall TEditMessage(TComponent* Owner);
__fastcall TEditMessage(TEditProfile *AProfile); // Added by user;
this looks like a duplication;
};
---------------------------------------------------------------------------
Source for EditMessage
---------------------------------------------------------------------------
#include <vcl.h>
#include <memory>
#pragma hdrstop
#include "editMessageDialog.h"
#include "editDialog.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TEditMessage *EditMessage;
//--------------------------------------------------------------------------
-
__fastcall TEditMessage::TEditMessage(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------
-
void __fastcall TEditMessage::MemoChange(TObject *Sender)
{
// Prevent the OK button from being clicked when the 'Memo' is empty.
if(Memo->GetTextLen()>0)
Ok->Enabled = true;
else
Ok->Enabled = false;
}
//--------------------------------------------------------------------------
-
void __fastcall TEditMessage::OkClick(TObject *Sender)
{
// Enable and check the message checkbox on the EditProfile dialog.
EditProfile->ShowMessage->Enabled = true;
EditProfile->ShowMessage->Checked = true;
}
//--------------------------------------------------------------------------
-
__fastcall TEditMessage::TEditMessage(TEditProfile *AProfile) // Added by
user
: TForm(Application), EditProfile(AProfile)
{
}
//--------------------------------------------------------------------------
Thanks again!
 

Re:Need Help Accessing Dynamic Form

"oLiVeS" < XXXX@XXXXX.COM >wrote in message
Quote
private: // User declarations
TEditProfile *EditProfile; // Added by user;
compiler error!
Did you include TEditProfile's header file into TEditMessage's header file?
Quote
public: // User declarations
__fastcall TEditMessage(TComponent* Owner);
__fastcall TEditMessage(TEditProfile *AProfile); // Added by user;
this looks like a duplication;
Remove the first one altogether.
Gambit
 

Re:Need Help Accessing Dynamic Form

Ok Remy, I got it all working now; thanks for your help.