Question

How to create an event of clicking on ANY of four buttons, i.e. avoid pasting same code into each button's OnClick event? The only thing I found in Google is that there is (?) a pattern (Multicast) for it. If it really exists, could you please provide me some examples, if not, could you make (a draft of) one?

Was it helpful?

Solution

You can assign the same handler to all four button OnClick events. The event's Sender parameter will tell you which button is triggering the event each time. For example:

void __fastcall TMyForm::ButtonClick(TObject *Sender)
{
    TButton *btn = static_cast<TButton*>(Sender);
    ShowMessage("You clicked on " + btn->Name);
}

If your code needs to behave depending on which button is clicked, you can use the Tag property to differentiate, for example:

void __fastcall TMyForm::ButtonClick(TObject *Sender)
{
    TButton *btn = static_cast<TButton*>(Sender);
    switch (btn->Tag)
    {
        ...
    }
}

OTHER TIPS

Assuming that the Button1->OnClick have been assigned :

Button2->OnClick = Button1->OnClick
Button3->OnClick = Button1->OnClick
Button4->OnClick = Button1->OnClick
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top