Setting OnClick methods in Borland C++ Builder

Posted on 2004-10-30
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

    Accepted Solution


    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.

    LVL 7

    Author Comment

    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 ;)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This article will show, step by step, how to integrate R code into a R Sweave document
    A short article about a problem I had getting the GPS LocationListener working.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now