Board index » cppbuilder » How to create a form with a button to click on dynamically?

How to create a form with a button to click on dynamically?


2005-06-05 04:41:12 AM
cppbuilder41
How to create a form with a button to click on dynamically?
Trying the following
class _MyForm: public TForm {
public:
_MyForm(TObject *Owner): TForm(Owner) {
}
void __fastcall MyButtonClick(System::TObject* Sender) {
Beep();
}
};
//
bool Fct(void) {
_MyForm *form=new _MyForm((TComponent *)NULL);
//
TButton *button=new TButton(form);
button->Parent=form;
button->OnClick=form->MyButtonClick;
...
}
does compile fine, but unfortunately I get an EResNotFound-exception,
telling me 'Resource _MyForm not found'.
Did I forget something or how else could I create a form with a button
and an OnClick-method dynamically?
Thanks a lot,
Michael
 
 

Re:How to create a form with a button to click on dynamically?

At 22:41:12, 04.06.2005, M_R wrote:
Quote
does compile fine, but unfortunately I get an EResNotFound-exception,
telling me 'Resource _MyForm not found'.
Did you actually use the New / Form menu to create that form file? I
guess a link to the .dfm file is missing.
--
Rudy Velthuis [TeamB] velthuis.homepage.t-online.de
"Defining and analyzing humor is a pastime of humorless people."
-- Robert Benchley (1889 - 1945)
 

Re:How to create a form with a button to click on dynamically?

<M_R>wrote in message news: XXXX@XXXXX.COM ...
Quote
Trying the following
There are several problems with that code.
Quote
class _MyForm: public TForm {
You should not prefix your own type names or variable names with
underscores. That is reserved for the compiler's own use.
Quote
_MyForm(TObject *Owner): TForm(Owner) {
You did not specify the __fastcall calling convention. Also, TForm's
constructor cannot accept a TObject parameter. It expects TComponent
instead. That code shouldn't even compile.
Quote
_MyForm *form=new _MyForm((TComponent *)NULL);
You specified _MyForm's constructor to take a TObject parameter but then are
passing it a TComponent instead. The only reason that compiles is because
TComponent derives from TObject.
Quote
TButton *button=new TButton(form);
button->Parent=form;
button->OnClick=form->MyButtonClick;
That code is better utilized by moving it inside the _MyForm class. Have
the TButton be a member of the class, and have _MyForm's constructor
instnatiate it.
Quote
does compile fine, but unfortunately I get an EResNotFound-exception,
telling me 'Resource _MyForm not found'.
Your _MyForm constructor is calling the TForm constructor that was designed
to be used with the DFM system. Your class is not using the DFM at all, so
you cannot call the TForm(TComponent*) constructor. You should be calling
the TForm(TComponent*, int) constructor instead, which bypasses the DFM
streaming.
Gambit
 

{smallsort}

Re:How to create a form with a button to click on dynamically?

Thanks, Gambit,
alll works fine now...
Michael