首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Borland C++ Builder上创建一组按钮并使用它?

如何在Borland C++ Builder上创建一组按钮并使用它?
EN

Stack Overflow用户
提问于 2012-09-08 05:56:54
回答 3查看 10.6K关注 0票数 0

具体问题

如何在Borland C++ Builder上创建一组按钮并使用它?

我使用的是Borland C++ Builder6和Borland Developer Studio2006 (Turbo C++ 2006)。

目的

只需使用带有索引的for循环就可以处理窗体上的许多按钮,例如,更改它们的标题、大小和位置。

我知道如果我有一个名为Button1的按钮,并且在这个按钮的单击事件中,如果我创建了另一个按钮(通过TButton *Button2 = new TButton(Form1)),我可以将Button1赋值给Button2 (Button2 = Button1),我可以简单地用Button2->Caption修改Button1的标题。所以我想对它进行扩展,将实部的指针赋给数组的元素,让它们与所有元素一起工作,使用一个for循环。

如果有人找到了一种方法,可以将所有按钮作为一个数组添加到表单上,那就更好了:)

尝试

以下测试将相应的代码放在TForm1::Button1Click()上,TForm1::Button1Click()是表单上按钮的事件:

  • 测试1

代码语言:javascript
复制
- Description: Creating an array directly
- Code:

TButton Buttons3;

-结果:编译error:C++错误Unit1.cpp(23):E2248找不到默认构造函数>来初始化类型为'TButton‘的数组元素

代码语言:javascript
复制
- Comments: 
    - I tested some variants of this test (e.g. `TButton Buttons = new TButton[3]`, working with `calloc` function and others), but all of them points to the issue that `TButton` does not have a constructor without arguments, i.e., `TButton()`, but only `TButton (TComponent *AOwner)`, `TButton(void *ParentWindow)` and `TButton(const TButton &)`;
    - Any way to use operator `new` with arguments for `TButton` constructor prototypes, for an array?

  • 测试2

求救矢量按钮;Buttons.Caption="it is ok";Buttons1.Caption=“求救,求救”;

-结果:调试器异常在第3行:Project Project1.exe引发异常类EAccessViolation >,并显示消息'Acceess violation at address 401075B9 in module > 'vcl60.bpl‘。读取地址00000254‘。进程已停止。使用> Step或Run继续。

代码语言:javascript
复制
- Comments: 
    - Yeah, I expected that it would be raised, but I put it here to someone say how to allocate memory for more elements on that vector after created, since `vector<TButton> Buttons(3);` does not work for the same reason test1 failed :(

一般问题

如何对任何可视组件执行此操作?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-09-11 04:43:28

由于相同的原因,您所有的尝试都失败了-您正在尝试创建一个实际TButton对象实例的数组/向量,而不是指向TButton实例的指针的数组/向量。

创建按钮指针的定长数组

代码语言:javascript
复制
TButton* Buttons[3];
...
Buttons[0] = Button1;
Buttons[1] = Button2;
Buttons[2] = Button3;
...
for(index = 0; index < 3; ++index)
{
    TButton *Btn = Buttons[index];
    // use Btn as needed...
}

创建按钮指针的动态长度数组

代码语言:javascript
复制
TButton** Buttons;
...
Buttons = new TButton*[3];
Buttons[0] = Button1;
Buttons[1] = Button2;
Buttons[2] = Button3;
...
for(index = 0; index < 3; ++index)
{
    TButton *Btn = Buttons[index];
    // use Btn as needed...
}
...
delete[] Buttons;

创建按钮指针的向量

代码语言:javascript
复制
std::vector<TButton*> Buttons;
...
Buttons.push_back(Button1);
Buttons.push_back(Button2);
Buttons.push_back(Button3);
...
for(index = 0; index < 3; ++index)
{
    TButton *Btn = Buttons[index];
    // use Btn as needed...
}
/*
Or:
for(std::vector<TButton*>::iterator iter = Buttons.begin(); iter != Buttons.end(); ++iter)
{
    TButton *Btn = *iter;
    // use Btn as needed...
}
*/
票数 3
EN

Stack Overflow用户

发布于 2018-07-01 02:12:48

所有这一切都很好,而且确实是真的。但我的意思是,用户的问题是另有目的的。如果对于我们得到的所有按钮,索引都没有特别的好处--这只是一个真正的方法:目的是通过点击来控制所有的组件(按钮,面板,形状等等…)不要为每个索引编写新的代码,这就是为什么我更改了几个程序的代码:

代码语言:javascript
复制
void __fastcall TForm1::Button1Click(TObject *Sender)

{

typedef TPanel* TPanels;

TPanels Panels[3] = {Panel1, Panel2, Panel3};

int count;

for(count=0;count<3;count++)

Panels[count]->Left=random(100);

}  

正如可以看到的,这里是count,而不是index。当然,不要忘记在TForm1中插入randomize()

票数 1
EN

Stack Overflow用户

发布于 2012-09-08 07:38:40

神奇的Typedef +伪数组=解决方案

  • 神奇的类型定义:

代码语言:javascript
复制
- After hours searching a way, I saw a `typedef` on that Stack Overflow and Google search journey and thought why not to:

类型定义TButton* TButtons;

-嗯,它改变了所有的事情,因为我可以执行:

TButtons Buttons3;

  • 伪数组:

代码语言:javascript
复制
- The issue remained on how to allocate memory for data stored on that `Buttons[3]` array, but with knowledge of 2nd paragraph of Purpose section of my question, I thought: forget new data, data is there, point to there (so I call that to build a pseudo array, because I create only an array of pointers to existing data):

TButtons Buttons3 = {Button1,Button2,Button3};

+当我正常地(通过鼠标)将Button1Button2Button3放在窗体上时,它们已经被创建了。

工作示例

  1. 创建新vcl/forms应用程序项目;
  2. 放置3个按钮(如下图所示)左侧的按钮(Button1Button2Button3)以演示解决方案,并将1个很棒的按钮(Button4) (也如下图所示)用于执行操作;

G222

  1. 在第四个按钮,即大按钮(Button4)的单击事件上插入以下代码;

类型定义TButton* TButtons;TButtons Buttons3 = {Button1,Button2,Button3};int index;for(index=0;index<3;index++) { Buttonsindex->Caption=(AnsiString)"teste "+index+“- "+(1+random(100));Buttonsindex->Left=25+4*随机(100);Buttonsindex->Top=25+4*random(100);}

  • 执行"shazam!”跑去玩那个..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12325918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档