Specific question
How to create an array of buttons on Borland C++ Builder and work with it?
I'm using Borland C++ Builder 6 and Borland Developer Studio 2006 (Turbo C++ 2006).
Purpose
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.
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 :)
Tries
Following tests were made putting respective code on TForm1::Button1Click(), an event of a button on a form:
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'
- Comments:
- 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?
- I tested some variants of this test (e.g.
Test 2
- Description: Creating a vector
Code: Also add
#include "vector.h"
on unit header...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.
- 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 :(
- 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
General question
How to do it for any visual component?