Use vector to use vector array in BCB

zhaozj2021-02-11  173

A few days ago, I asked such a question: "Five Button's OnClick events correspond to the name of the five Label", because the BCB has no control array, so everyone's answer is mostly RTTI, yesterday When I read "C Primer", I suddenly remembered the VECTOR type in the standard library. We also use this type to implement an array of controls.

Today, I tried it, I wrote the process of the experiment here. First include the header file and set the namespace, declare the variable. In mainform.h:

#include

USING NAMESPACE STD // Standard Library's namespace

Declaring Vector TEST in the PUBLIC of Form1;

Note that here I want to initialize the array in the form of Vector Test (5), but BCB seems to do not support this form, and the problems in this regard are to be resolved.

Then add two button and a label on the main form, and in the event of Button1, this array is initialized.

For (int i = 0; i <5; i )

{

TLABEL * label = new TLABEL (this);

Label-> Parent = Form1;

Label-> TOP = 20 * i;

Test.push_back (label);

}

Test.push_back (testLabel); // Test if the array can contain a static generated control. The array at this time should be included in 6 member variables.

Write the following code in Button2:

For (int i = 0; i

{

TEST [I] -> CAPTION = "Wolf" Ansistring (i):

}

This example generates an array of controls by assigning a array cycle, and operates this array through a loop. The use of the VECTOR data type is only a very shallow, and C STL technology can give our programming greater freedom and elasticity.

The above code is compiled in the Win2000 and BCB5 environments.

转载请注明原文地址:https://www.9cbs.com/read-5074.html

New Post(0)