BCB6: How to put elements of a form in an array?
I'm building a simple game in C++Builder6 and I have 42 Image objects on a Form... At start-up I want all Image objects to be disabled, so I wonder can I put all of 开发者_高级运维them in an array and simply loop thorough the entire array and make them Disabled? I know there must be a way, but I'm just new to programming :)
You have several options. First: You can declare
Image* array[40];
And dynamically construct the image.
for ( int i = 0 ; i < 40; ++i ) {
image[i] = new Image(this); // where "this" is pointer to your form
image[i]->Parent = this;
// option below are optional
image[i]->Height = 50;
image[i]->Width = 50;
image[i]->Left = 40;
image[i]->Top = 100;
image[i]->Tag = i;
image[i]->OnClick = ButtonClick; // connect with method
}
Second option is declare
Image* array[40];
and manually set all values;
array[0] = Image1;
...
array[39] = Image40;
Then you will have all image in array and you can use loop for doing something on all Image
精彩评论