To sto tebi treba se u BCB-u odradjuje vrlo jednostavno. Potrebno je samo napraviti
prototip te surogat funkcije i izvrsiti dodelu:
--- H ---
Code:
class TForm1 : public TForm
{
__published: // IDE-managed Components
TEdit *Edit1; // <--- TEdit na koji ce se odnositi novi OnChange
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall MyOnChange(TObject *Sender); // <--- Novi OnChange()
};
--- CPP ---
Code:
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Edit1->OnChange = MyOnChange; // <--- Dodela novog dogadjaja starom
}
void __fastcall TForm1::MyOnChange(TObject *Sender)
{
ShowMessage( "Moj on change" ); // <--- Novi dogadjaj
}
E sad, ovo gore radi, ali je je ruzno. Lepse je napraviti da cela tvoja zamisao bude deo neke
nove TEdit kontrole. Kako se prave nove komponente, pa kako se one jos mogu 'ugradiri' u IDE,
da se vide u paleti sa alatkama - prevazilazi domen ovog threada. To moras da pogledas u nekoj
knjizi ili na netu.
Za sada evo malo koda da te zainteresuje:
--- H ---
Code:
class TNewEdit : public TEdit
{
private:
void __fastcall NewOnChange(TObject *Sender);
protected:
public:
AnsiString Tip;
__fastcall TNewEdit(TComponent* Owner);
};
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
TNewEdit *pNewEdit;
};
--- CPP ---
Code:
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
pNewEdit = new TNewEdit( this );
pNewEdit->Parent = this;
pNewEdit->Tip = "proba";
}
__fastcall TNewEdit::TNewEdit(TComponent* Owner)
: TEdit( Owner )
{
Caption = "NewEdit";
OnChange = NewOnChange;
};
void __fastcall TNewEdit::NewOnChange(TObject *Sender)
{
if ( Tip == "proba" )
ShowMessage( "proba" );
else
ShowMessage( "nije proba" );
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
pNewEdit->Tip = "bilo sta drugo"; // <--- Promena tipa, tek da se vidi da OnChange odradjuje drugo...
}
I na kraju. Nikad u BCB-u ne koristi OnCreate() i OnDestroy() dogadjaje:
Citat:
Ah ha! The short answer is - don't! OnCreate and OnDestroy are Delphi-isms
that should be avoided like the plague in C++ code. Here is Chris Uzdavinis
excellent post on this:
OnCreate is a terrible decision to use, and it should not be there at
all except that Delphi code uses it. Other than the order of being
called, there are several other problems as well. In C++ code, I beg
and plead people to not use it as if OnCreate didn't exist.
In addition to the unpredictable calling order (well, it is
predictable but still surprises a lot of people), there are some other
significant problems with these events. These are major risks and
should not be taken lightly.
* derived objects won't have the base OnCreate called when they are
created (unless users explicitly call it.)
* dynamic instantiation (in program code) of a object will NOT call
the OnCreate event. [problem is fixed in bcb 4 or 5 (not sure
which), but exists in older versions
* there is no initializer list for OnCreate, so if OnCreate is all
that is used only objects that have default constructors can be
used. If you have non-default constructors that need to be called,
you *have* to put the code in the true constructor. If the
constructor must pass another pointer member to that constructor,
and that pointer is initialized in OnCreate, it is potentially not
initialized at the time of the constructor and the application will
crash. If your class holds a reference to an object, then you can't
use OnCreate to initialize the reference, you MUST use a
constructor.
* being a normal function, your OnCreate handler can be called
multiple times (accidently, of course) which an potentially be
disastrous.
* Copy construction doesn't call OnCreate at all. (However, in BCB5
copy construction of vcl objects is prevented at compile time, older
versions did not.)
* THE INITIALIZATION DONE BY FORMCREATE CAN POSSIBLY RUN *BEFORE* YOUR
CONSTRUCOTR, WHICH MEANS THAT YOUR ENTIRE PROGRAM BEHAVIOR IS
POSSIBLY UNDEFINED. This is an ***EXTREMELY SERIOUS*** problem.
To su Delphi idiomi, i imaju istoriju neobicnog ponasanja u BCB-u. One su u raskoraku sa
pravilima C++ jezika o redosledu pozivanja konstruktora i destruktora, pogotovo ako
krenes da pravis svoje komponente.
Umesto njih koristi konstruktor (IDE ga odmah pravi) i destruktor (moras ga sam napraviti).