• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2015
  • Last Modified:

Setting OnClick methods in Borland C++ Builder

I am using Borland C++ Builder and am created an object (in this specific instance, a TLabel) in runtime:
class PreviewItem
{
private:
  TPanel    *Container;
  TLabel    *SelMask;
   ...more...
public:
   PreviewItem(TPanel *Parent, String ImageSource, String CaptionText)
  {
     ...create Container and other componants...
    SelMask = new TLabel(Container);
    SelMask->Parent = Container;
    ...set proterties for SelMask...
  }
};
I want to set it's OnClick method to a function within the PreviewItem class. How would I do this?
0
icr
Asked:
icr
1 Solution
 
acarrierCommented:
Hi,

You have to assign the onlick event handler of the label to a function you create. For a TControl (wich tlabel descends from) the OnClick event handler is basically a function pointer you want to assign. You assign your function yo that pointer. The function must be according to the doc :

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

This means that in your previewitem class, you have to declare a function like this:
// declaration, in your header file.
void __fastcall PreviewItem::MyOnClickFunction(TObject* Sender);
// function definition
void __fastcall MyOnClickFunction(TObject* Sender)
{
           // sender is your Tlabel object so you can access it in this function
           // do your thing here.
      // you can try this to confirm all this works just as a test:
      ShowMessage("YAHOO");
};

LAST STEP:
Now in your previewitem constructor, assign the onclick event handler to your function:
SelMask->OnClick = MyOnClickFunction;

I have tried this code and it works on my side.
Let us know.

Later!
A.Carrier
0
 
icrAuthor Commented:
Yay, works. Thanks. I did actually have all of that code, except when I was trying to assign the OnClick function I was trying to parse Sender. I suppose I should have mentioned that :) But thanks, great explination too ;)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now