C++Builder VCL array of buttons dynamically made each with his independent click action

烂漫一生 提交于 2020-01-05 10:22:05

问题


i have tried the following way but it doesn't seem to work and it is incomplete.

void __fastcall TForm1::Button1Click(TObject *Sender) 
{
    TButton **ButtonArray = NULL;
    ButtonArray = new TButton*[5];

    for(int x = 0; x < 5; ++x) 
    {
        ButtonArray[x] = new TButton(this);
        ButtonArray[x]->Caption = (AnsiString)"teste " + x + " - " + (1+random(100));
        ButtonArray[x]->Left = 25 + 4 * random(100);
        ButtonArray[x]->Top = 25 + 4 * random(100);
    }
}

Code compiles without problems, but no button seems to show. Also, there is no action and buttons in the array have a predefined max of 5.


回答1:


To make them visible in your form you must set the Parent property of your buttons.

ButtonArray[ x ]->Parent = this;

There's no action because you do not set one.

void __fastcall TForm1::ButtonClick(TObject *Sender)
{
   MessageBox( Handle, L"Hello", L"Message", MB_OK );
}

And when creating your buttons...

ButtonArray[ x ]->OnClick = ButtonClick;


来源:https://stackoverflow.com/questions/17274708/cbuilder-vcl-array-of-buttons-dynamically-made-each-with-his-independent-click

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