how to write a function Click() for dynamic created button?

旧街凉风 提交于 2019-12-06 15:18:39

问题


Trying to write a simple VCL program for educating purposes (dynamicly created forms, controls etc). Have such a sample code:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TForm* formQuiz = new TForm(this);
    formQuiz->BorderIcons = TBorderIcons() << biSystemMenu >> biMinimize >> biMaximize;
    formQuiz->Position = TPosition::poDesktopCenter;
    formQuiz->Width = 250;
    formQuiz->Height = 250;
    formQuiz->Visible = true;

    TButton* btnDecToBin = new TButton(formQuiz);
    btnDecToBin->Parent = formQuiz;
    btnDecToBin->Left = 88;
    btnDecToBin->Top = 28;
    btnDecToBin->Caption = "Dec to Bin";
    btnDecToBin->Visible = true;
}

I wonder how can i write a function for dynamic created button, so it would be called when the button is clicked. In this example i need a 'btnDecToBin->Click();' func but i don't know where should i place it.

Inside 'void __fastcall TForm1::Button1Click(TObject *Sender){}' ?

I will appreciate any input, some keywords for google too.


回答1:


You could do two things, you could either create an action and associate it with the button, or you could make a function like so:

void __fastcall TForm1::DynButtonClick(TObject *Sender)
{
    // Find out which button was pressed:
    TButton *btn = dynamic_cast<TButton *>(Sender);

    if (btn)
    {
        // Do action here with button (btn).
    }
}

You bind it to the button instance by setting the OnClick property btnDecToBin->OnClick = DynButtonClick please note that the function is inside the form Form1. This will work due to the nature of closures (compiler specific addition). The problem comes if you delete Form1 before formQuiz without removing the reference to the click event. In many ways it might be a more clean solution to use an Action in this case.

Edit: On other way to do this, if you have a standard layout for your quizforms, you could make a custom TQuizForm class inheriting from TForm. In this way you wouldn't have to bind the event each time you create the form.




回答2:


all buttons have the normal "events" you just need to reference them to the method you will deal with the event.

example:

...    
btnDecToBin->OnClick = &Test;

-- and add a additional method to .cpp

void __fastcall TForm1::Test(TObject *Sender)
{

   TButton *btn = dynamic_cast<TButton *>(Sender);

    if (btn->name == "your_button_name"){         
    // Do action here with button (btn).     
    }

}

and on .h

void __fastcall TForm1::Test(TObject *Sender);

reference the button either by the tag or name. I usually use a array of buttons that I create dynamically. ALWAYS sanity check your "sender" by casting it. There are other ways to hack info from the object but they are a path to heartache... LOL.



来源:https://stackoverflow.com/questions/2116959/how-to-write-a-function-click-for-dynamic-created-button

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!