Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Setting OnClick methods in Borland C++ Builder

Posted on 2004-10-30
Medium Priority
Last Modified: 2010-10-05
I am using Borland C++ Builder and am created an object (in this specific instance, a TLabel) in runtime:
class PreviewItem
  TPanel    *Container;
  TLabel    *SelMask;
   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?
Question by:icr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

acarrier earned 300 total points
ID: 12452403

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:

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.


Author Comment

ID: 12452660
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 ;)

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Introduction to Processes

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question