如何为动态创建的按钮编写函数Click()?
尝试编写一个简单的 VCL 程序用于教育目的(动态创建的表单、控件等)。有这样的示例代码:
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;
}
我想知道如何为动态创建的按钮编写一个函数,以便在单击按钮时调用它。在此示例中,我需要一个 'btnDecToBin->Click();' func 但我不知道应该把它放在哪里。
在“void __fastcall TForm1::Button1Click(TObject *Sender){}
”内?
我将不胜感激任何意见,还有谷歌的一些关键字。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以做两件事,您可以创建一个操作并将其与按钮关联,也可以创建一个如下所示的函数:
通过设置 OnClick 属性
btnDecToBin->OnClick = DynButtonClick 将其绑定到按钮实例
请注意,该函数位于 Form1 表单内。由于闭包的性质(编译器特定的添加),这将起作用。如果您在formQuiz
之前删除Form1
而没有删除对 click 事件的引用,就会出现问题。从很多方面来说,在这种情况下使用操作可能是一个更干净的解决方案。编辑:以其他方式执行此操作,如果您的测验表单有标准布局,则可以创建一个继承自
TForm
的自定义TQuizForm
类。这样,您就不必在每次创建表单时都绑定事件。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:
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 deleteForm1
beforeformQuiz
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 fromTForm
. In this way you wouldn't have to bind the event each time you create the form.所有按钮都有正常的“事件”,您只需将它们引用到您将处理事件的方法即可。
示例:
-- 向 .cpp 添加一个附加方法
,并在 .h 上
通过标签或名称引用按钮。我通常使用动态创建的一系列按钮。始终通过投射来检查您的“发件人”。还有其他方法可以从对象中获取信息,但它们会导致心痛……哈哈。
all buttons have the normal "events" you just need to reference them to the method you will deal with the event.
example:
-- and add a additional method to .cpp
and on .h
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.