问题描述
如何在Borland C ++ Builder上创建按钮数组并使用它?
How to create an array of buttons on Borland C++ Builder and work with it?
我正在使用Borland C ++ Builder 6和Borland Developer Studio 2006(Turbo C ++ 2006)。
I'm using Borland C++ Builder 6 and Borland Developer Studio 2006 (Turbo C++ 2006).
仅使用带有索引的for循环来处理表单上的许多按钮,例如,更改其标题,大小和位置。
To work with a lot of buttons on a form just using a for loop with an index, for example, changing their caption, size and position.
我知道是否如果我创建另一个按钮(通过 TButton * Button2 = new TButton(Form1),则有一个名为
Button1
的按钮,并且在此按钮的click事件内) code>),我可以将 Button1
分配给 Button2
( Button2 = Button1
),而我只需用 Button2->标题
修改 Button1
的标题。因此,我想扩展它,将真实组件的指针分配给数组元素,并通过 for
循环将它们与所有组件一起使用。
I know if I have a button called Button1
and inside a click event of this button if I create another button (through TButton *Button2 = new TButton(Form1)
), I can assign Button1
to Button2
(Button2 = Button1
) and them I can simply modify caption of Button1
with Button2->Caption
. So I would like to extend it assigning pointers of real components to elements of an array to them work with all of them with a for
loop.
好吧,如果有人找到了一种将所有按钮添加为表单上的数组的方法,那就更好了:)
Well, if someone found an way to add all buttons as an array on a form, it's better :)
进行了以下测试,将相应的代码放在TForm1 :: Button1Click()上,这是表单上按钮的事件:
Following tests were made putting respective code on TForm1::Button1Click(), an event of a button on a form:
-
测试1
Test 1
- 说明:直接创建数组
-
代码:
- Description: Creating an array directly
Code:
TButton Buttons[3];
结果:编译错误:
Result: Compile error:
> [C++ Error] Unit1.cpp(23): E2248 Cannot find default constructor
> to initialize array element of type 'TButton'
- 我测试了此测试的一些变体(例如,
TButton Buttons = new TButton [3]
,与calloc
和其他函数),但是所有这些都指出了TButton
没有没有参数的构造函数的问题,即TButton()
,但只有TButton(TComponent * AOwner)
,TButton(void * ParentWindow)
和TButton(const TButton&)
; - 使用运算符
new
的任何方式TButton
构造函数原型的参数,用于数组吗?
- I tested some variants of this test (e.g.
TButton Buttons = new TButton[3]
, working withcalloc
function and others), but all of them points to the issue thatTButton
does not have a constructor without arguments, i.e.,TButton()
, but onlyTButton (TComponent *AOwner)
,TButton(void *ParentWindow)
andTButton(const TButton &)
; - Any way to use operator
new
with arguments forTButton
constructor prototypes, for an array? - 描述:创建向量
-
代码:还要在单元标题上添加
#include vector.h
...
测试2
vector<TButton> Buttons;
Buttons[0].Caption="it is ok";
Buttons[1].Caption="mayday, mayday";
结果:第三行的调试器异常:
Result: Debugger exception on 3rd line:
> Project Project1.exe raised exception class EAccessViolation
> with message 'Acceess violation at address 401075B9 in module
> 'vcl60.bpl'. Read of address 00000254'. Proccess stopped. Use
> Step or Run to continue.
- 是的,我希望它会被提出,但是我在这里向某人说,创建后如何为该向量上的更多元素分配内存,因为
vector< TButton> Buttons(3);
由于test1失败的相同原因而无法工作:(
- 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 :(
对于任何视觉组件,该如何做?
How to do it for any visual component?
推荐答案
由于相同的原因,所有尝试均以失败告终-您正在尝试创建实际 TButton
对象实例,而不是 TButton
实例的指针的数组/向量。
All of your attempts failed for the same reason - you are trying to create an array/vector of actual TButton
object instances instead of an array/vector of pointers to TButton
instances.
要创建按钮的固定长度数组指针:
To create a fixed-length array of button pointers:
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...
}
创建按钮指针的动态长度数组:
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;
创建按钮指针的向量:
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...
}
*/
这篇关于如何在Borland C ++ Builder上创建按钮数组并使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!