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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnMouseDown for control created dynamicaly

Status
Not open for further replies.

zvikorn

Programmer
Aug 6, 2003
17
0
0
US
Hello,
I would like to create a CheckBox dynamicaly when the form is created.
The code will look like that:

TCheckBox *cBox=new TCheckBox (MainForm);
cBox->Name=......;
cBox->Parent = MainForm;
.
.
.

Q: I want to do something when the user click the CheckBox at runtime.
Since the CheckBox was not created as design time, there is no function called 'CheckBox1Click(......)' to override and write my code in it.
How do I write my code , for this scenario then?
How do I write my code for events for controls that are created at runtime?
Thanks a lot.
Tzvika

 
Well, i'm not totally shure HOW but i know that You write the code and when You create the dynamic whatever You assign the adress of the code to it, it's something like:

void Doing_the_voodoo_thing(with the correct parameters)
{
// Do a lot of stuff here
}

and then when You create Your dynamic component You assign Doing_the_voodoo_thing to the event of the component and behold, they're connected.

At least it's what i've understood so far but i do not understand why You has to create the component dynamically, it could be on some form but invisible and then turned visible when needed.

Totte
 
Totte has the right idea.

void DynamicClick(TObject *Sender)
{
TCheckBox *Box = (TCheckBox*) Sender;
AnsiString NameOfBox = Box->Name;
}

... some other function ...
{
...
TCheckBox *NewBox = new TCheckBox(this);
NewBox->OnClick = DynamicClick;
...
}

Chris
 
Just as a little add-on to what Supernat03 wrote.
If you want to use the OnMouseDown-event the code would be something like this:

void __fastcall DynamicClick(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
//Stuff
}

//...

//code where the CheckBox is created
TCheckBox *NewBox = new TCheckBox(this);
NewBox->OnMouseDown = DynamicClick;


I'm not 100% sure "__fastcall" is needed, but someone advised me to only use it with events - so that I do :)

ApJ
 
About __fastcall:

This is requiered, since TControl::OnClick is declared as TNotifyEvent which has the prototype like that:

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

The __fastcall directive means "register calling function" opposite the __stdcall which means "standard (stack based) calling function" (see the online help for more info). If you declare the function without __stdcall or __fastcall the calling mechanism is determined from Project Options page - Addvanced Compiler Options.

unbornchikken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top