Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Borland C++ Builder: Kako programski menjati evente neke kontrole?

[es] :: C/C++ programiranje :: Borland C++ Builder: Kako programski menjati evente neke kontrole?

[ Pregleda: 2105 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

popmilo
Vladimir Jankovic
Senta

Član broj: 126810
Poruke: 6
*.sabotronic.co.yu.

Sajt: pc.sux.org


Profil

icon Borland C++ Builder: Kako programski menjati evente neke kontrole?24.12.2006. u 00:36 - pre 211 meseci
Hteo sam da napravim custom EditBox-ove, više tipova... (string polje, number, datumsko) ali tako da svaka vrsta polja drugačije reaguje.

Hteo bih da kreiram formu sa proizvoljnim brojem ovakvih mojih kontrola, tako da im je ponašanje određeno recimo njihovim (custom) propertyjem "tip"...

Ove kontrole ne bih dodavao ručno na formu nego bi ih programski kreirao na FormCreate događaju (pošto ih je proizvoljan broj)

I sad recimo u OnClick event da upišem:

if (tip=="datum") { nešto }
else {nešto drugo... :) }

Mislim da u ovom slučaju moje pitanje glasi:

Kako da u definiciju klase (nove custom multifunkcionalne komponente) upišem taj source kod tog mog "multifunkcionalnog" eventa ?
event kao property trazi nesto tipa:
(__fastcall * (__closure )(TObject *))(TObject *)
Ali ne mogu da potrefim pravi način da mu dodelim vrednost...

Izgleda sam zarđao u C++-u jer su mi od ove konstrukcije jasne samo zagrade :)



[Ovu poruku je menjao popmilo dana 24.12.2006. u 09:44 GMT+1]
 
Odgovor na temu

X Files
Vladimir Stefanovic
Pozarevac

SuperModerator
Član broj: 15100
Poruke: 4902
*.dynamic.sbb.co.yu.

Jabber: xfiles@elitesecurity.org


+638 Profil

icon Re: Borland C++ Builder: Kako programski menjati evente neke kontrole?24.12.2006. u 09:37 - pre 211 meseci
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).

 
Odgovor na temu

popmilo
Vladimir Jankovic
Senta

Član broj: 126810
Poruke: 6
*.SMIN.panline.net.

Sajt: pc.sux.org


Profil

icon Re: Borland C++ Builder: Kako programski menjati evente neke kontrole?24.12.2006. u 18:44 - pre 211 meseci
Hvala na objasnjenju... probacu veceras...

Sve sam ja ukucao kao u helpu, kreirao novu komponentu, napravio novu klasu kao u examples, stavio komponentu u toolbar .... ali iz nekog glupog razloga koji ja ne vidim - jednostavno ne radi.... :(

Sve se lepo kompajlira ali ne reaguje na Click pa to ti je....

nadam se da ce uz ove tvoje savete uspeti....

javicu rezultate....
 
Odgovor na temu

popmilo
Vladimir Jankovic
Senta

Član broj: 126810
Poruke: 6
*.SMIN.panline.net.

Sajt: pc.sux.org


Profil

icon Re: Borland C++ Builder: Kako programski menjati evente neke kontrole?25.12.2006. u 19:49 - pre 211 meseci
Uspelo je! Hvala puno na objasnjenju...

Ima samo jedna nejasnoca...

Pokusao sam prvo sa imenom te nove klase "TPolje" i sve se lepo kompajlira bez greske, ali ne reaguje na dogadjaje.....
Onda samo promenim ime na "TNewLabeledEdit" posto sam je napravio kao naslednika TLabeledEdit kontrole i sve radi....ccc...

E sad, jeste da se nisam bavio c++/om pet,sest godina, ali ovakvo ponasanje mi nije poznato...

Moze li neko ovo da objasni?
 
Odgovor na temu

X Files
Vladimir Stefanovic
Pozarevac

SuperModerator
Član broj: 15100
Poruke: 4902
*.dynamic.sbb.co.yu.

Jabber: xfiles@elitesecurity.org


+638 Profil

icon Re: Borland C++ Builder: Kako programski menjati evente neke kontrole?25.12.2006. u 20:23 - pre 211 meseci
Nista ne mogu detaljnije reci dok ne vidim kod. Kako se klasa zove (ukoliko ime nije vec
upotrebljeno) nema bas nikakve veze.
 
Odgovor na temu

[es] :: C/C++ programiranje :: Borland C++ Builder: Kako programski menjati evente neke kontrole?

[ Pregleda: 2105 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.