 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Erik Doekes Guest
|
Posted: Wed Jan 19, 2005 7:46 am Post subject: Pass event function as constructor parameter? |
|
|
Hello!
-Is it possible to pass a function name as a parameter to a class
constructor which then can set the OnMoved property of one of the VCL
attributes of the class?
-How do I do that? (I mean, I can set XX->OnMoved=Form1->SplitterMoved, but
I don't know the "type" of a function, which would be required to be able to
pass it as an argument)
Maybe it's not even relevant, but for your background information here is my
problem in more detail:
I've created a VCL-based class called TransactionRow, which consists of a
panel with editing fields and splitters.
The instances of TransactionRow will be placed below each other on the
screen (as well as contained in a TList in the main form MainForm).
Now, the splitters have to interact - if one is moved, then all the others
do as well, as well as do the splitters of one instance of another class,
HeaderPanel. So I wrote a function instantiateSplitter which was called by
the constructors of both TransactionRow and HeaderPanel. Parameters are a
pointer to the spliter, the owner component, the parent control and its left
position. Among the properties set were Parent, Left and Minsize but also
OnMoved = MainForm->SplitterMoved.
So far so good. But now, I want to separate out TransactionRow into its own
unit file and here the problems start. TransactionRow.cpp will be called
before the main form has been instantiated and thus BC++ cannot find
MainForm. Placing the function instantiateSplitter inside the main unit will
not help either, since I then have to call MainForm->instantiateSplitter
from the TransactionRow class constructor.
Should I abandon my thoughts of separating out TransactionRow.cpp? I don't
like the idea of having to call a function from the main form each time a
TransactionRow is created.
An alternative might be to send the function OnMoved as a parameter to the
constructor of TransactionRow. That's an idea I really like, but don't know
how to do.
Even if you think I should use something else like e.g. a ListView, I would
appreciate a specific answer to the function parameter question.
Grateful for your help on this one,
/Erik
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Wed Jan 19, 2005 12:29 pm Post subject: Re: Pass event function as constructor parameter? |
|
|
Erik Doekes wrote:
| Quote: | -Is it possible to pass a function name as a parameter to a class
constructor which then can set the OnMoved property of one of the VCL
attributes of the class?
|
Yes. Demo follows where the constructor of TForm2 takes a function
of an instance of TForm1 as parameter:
Unit1.cpp
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage ( "Hello this is Button1");
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
TForm2 *Form2 = new TForm2 ( this, &Button1Click);
Form2->Show();
}
Unit2.hpp
typedef void __fastcall (__closure *TFunc)(TObject *Sender);
class TForm2 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
private: // User declarations
public: // User declarations
__fastcall TForm2(TComponent* Owner);
__fastcall TForm2(TComponent* Owner, TFunc );
};
Unit2.cpp:
__fastcall TForm2::TForm2(TComponent* Owner, TFunc Func)
: TForm(Owner)
{
Button1->OnClick = Func;
}
Hans.
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed Jan 19, 2005 6:15 pm Post subject: Re: Pass event function as constructor parameter? |
|
|
"Hans Galema" <notused (AT) notused (DOT) nl> wrote
| Quote: | typedef void __fastcall (__closure *TFunc)(TObject *Sender);
|
Use the VCL's existing TNotifyEvent type instead. Especilly since the
OnClick event is declared as a TNotifyEvent in the first place.
Gambit
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Thu Jan 20, 2005 12:54 pm Post subject: Re: Pass event function as constructor parameter? |
|
|
Remy Lebeau (TeamB) wrote:
| Quote: | typedef void __fastcall (__closure *TFunc)(TObject *Sender);
Use the VCL's existing TNotifyEvent type instead. Especilly since the
OnClick event is declared as a TNotifyEvent in the first place.
|
First did that indeed. But could not get it working so I decided
to do it from scratch. It was the & in &Button1Click which
I had not forseen and am still amazed of. Indeed TNotifyEvent
is the same typedef.
Hans.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|