Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AfterScroll Event in a derived class

Status
Not open for further replies.

hansaplast

Programmer
Dec 17, 2001
61
NL
Hi,

I'm trying to write an AfterScroll event for my TDBApp class but dunno
how to implement this.

I want to do someting like this in my App.

// MyApp.cpp
#include DBApp.h
void MyApp::somefunction()
{
TDBApp *pDBApp = new TDBApp(NULL);

// How do I implement this functionality??
pDBApp->AfterScroll = MyOwnAfterScrollFunction;
}

void MyApp::MyOwnAfterScrollFunction(/*With params*/){
// Do something...
}


// DBApp.h
class TDBApp : public TDatabase {
// ...
protected:
TTable *pArchive;
// ...
public:
// Overriding the default constructor
virtual __fastcall TDmsDB(Classes::TComponent* AOwner);
}

// DBApp.cpp
__fastcall TDmsDB::TDmsDB(Classes::TComponent*
AOwner):TDatabase(AOwner)
{
pArchive->AfterScroll = ArchiveAfterScroll;
// TDatabase Specific...
// pArchive->AfterScroll is fired as soon as the application
// scrolls from one record to another.
}

void __fastcall TDmsDB::ArchiveAfterScroll(TDataSet * DataSet)
{
if (pArchive->Active) {
// ...
// *HERE I want to FIRE the "DBApp::AfterScroll" Event
// to whatever function is attached to it
// How to do this???
// ...
}
}

Regards,

Hanza
 
As a simple example:

MyClass.h file
class MyClass : TObject
{
private:
TNotifyEvent FOnSomething;
public:
__published:
__property TNotifyEvent OnSomething = {read=FOnSomething, write=FOnSomething};
};

MainForm.h file
class TMainForm : TForm
{
//usual stuff here...
MyClass *Mine;
void __fastcall SomeFunction(void);
void __fastcall HandleOnSomething(TObject *Sender);
}

MainForm.cpp file
void __fastcall MainForm::SomeFunction(void)
{
Mine = new MyClass(SomeParameters);
Mine->OnSomething = HandleOnSomething;
delete Mine;
}

Now, assume you want to pass in parameters other than TObject *Sender.

Let's look at what TNotifyEvent really is. It's a function prototype.

typedef void __fastcall (__closure *TNotifyEvent) (System::TObject* Sender);

So, follow that concept with your own function. Say you want to pass in 2 integers to the event handler.

typedef void __fastcall (__closure *TOnSomethingEvent) (int Int1, int Int2);

Now just replace TNotifyEvent with TOnSomethingEvent in your class definition. Then replace the parameters in your main form's event handler function. That's all there is to it!

Good luck,
Chris
 
Thanks Super,

Got the idea.
Next problem is that I don't know how to fire the Event I've just created. How do I execute the function which "FOnSomething" points to?
 
Nevermind :)

The Sollution:
OnSomething(/*TObject*/);

Thankz anyway
 
Just to complete things. It could be handy for other programmers :)
Let me show you what I have:

// MyDB.h
(irrelevant function prototypes are left away)

class TMyDB : public TDatabase {
private:
// typedef void __fastcall (__closure *TDataSetNotifyEvent)(TDataSet* DataSet);
TDataSetNotifyEvent FAfterScroll;

protected:
void __fastcall AppAfterScroll(TDataSet *DataSet);

public:
void __fastcall FireScrollEvent(TDataSet * DataSet);

__published:
__property TDataSetNotifyEvent AfterScroll = { read=FAfterScroll, write=FAfterScroll };


// MyDB.cpp
#include "mydb.h"

// Assign "AfterScroll" a value in the constructor
__fastcall TMyDB::TMyDB(Classes::TComponent* AOwner):TDatabase(AOwner)
{
// Assign a function which handles the AfterScroll Event
// AfterScroll MUST HAVE A VALUE!
AfterScroll = AppAfterScroll;
}

// Just a function to point out how you can fire an AfterScroll Event
void __fastcall TMyDB::FireScrollEvent(TDataSet * DataSet)
{
AfterScroll(DataSet);
}


void __fastcall TMyDB::AppAfterScroll(TDataSet * DataSet)
{
/* This Function MUST EXIST. Its the default handler for the AfterScroll event*/
/* If AfterScroll is overridden. i.e. "AfterScroll = AnOtherFunction" this Function will be bypassed */
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top