An array of VCL controls in BCB (1)
Hoist
Last night and netizen Yan Yanhua chatted on OICQ, he said that he was editing a gaming menu for his friend, and dynamically created a set of press, but finally could not release. His implementation method is as follows:
For (int i = 1; i <= buttoncount; i )
{
TspeedButton * SPDBTN = New TspeedButton (this);
SPDBTN-> Parent = scrollbox; // Specify a parent control
SPDBTN-> CAPTION = INTOSTR (i);
SPDBTN-> Width = 80;
SPDBTN-> Height = 80;
SPDBTN-> OnClick = buttonclick;
SPDBTN-> LEFT = intLeft;
SPDBTN-> TOP = INTTOP;
SPDBTN-> GroupIndex = 1;
SPDBTN-> FLAT = true;
INTLEFT = INTLEFT 80 INTSPACE;
IF (i% linecount == 0)
{
INTTOP = INTTOP 80 INTSPACE;
INTLEFT = INTSPACE;
}
Buttons-> add (spdbtn); // Buttons is a TLIST pointer
}
Finally, the memory cannot be released with the Clear () method of TLIST.
In fact, the CLEAR () method is just emptying the list. To delete or have delete, but the delete operator must have a deleted pointer, but this implementation method cannot get a pointer! So I gave up this kind of thinking, suddenly, electric light flashed (not to be thundered, but I came up with a way), can I use array? Talk to dry! Array assignment? I think, right!
TspeedButton * Buttons [] = new tspeedbutton [4] (this);
But the compiler tells me: Error!
TspeedButton * Buttons [] = new tspeedbutton (this) [4]
Still wrong! Finally, I made a smile, took the Java allocation method:
TspeedButton [] * Buttons = New TspeedButton [] (this)
Do you? Don't say it! Is there any way? I think of a simple type of pointer array int x [] = {1, 2, 3};
TspeedButton * Buttons [] = {New TspeedButton (this), New TspeedButton (this), New TspeedButton (this)}
Actually! I am thinking about smiling, suddenly discovered: What if I want to define 100 buttons ... Who is it? Even if you use Copy / Parst, it is inevitable that the number is wrong, after all, 100 times. Is it no law? After hard thinking, I remembered a way. How is it step by step?
TspeedButton ** Button = new tbutton * [100];
For (int i = 0; i <100; i ) Button [i] = new tspeedbutton (this);
Haha! Actually OK! Try release:
For (int i = 0; i <4; i ) delete x [i];
DELETE [] X;
Haha! Actually OK! So I wrote an example: Put two button on a window, click on the dynamically generated button.
First, declare a global variable tbutton ** x;
Then add the generated code in the Button1 on /click:
x = new tbutton * [4];
For (int i = 0; i <4; i )
{
X [i] = new tbutton (this);
X [i] -> left = 100;
X [I] -> TOP = 10 i * 30;
X [i] -> width = 90;
X [I] -> Height = 25;
x [i] -> parent = this;
X [i] -> CAPTION = "Press New" ANSISTRING (i);
}
Click it to generate and display 4 buttons, then join the release code in Button2:
For (int i = 0; i <4; i ) delete x [i];
DELETE [] X;
Run a try, OK! Datual!
Therefore, the process using the VCL array is: first declare a dual pointer, then assign the number of the required VCL components, and finally allocate each VCL component; in the release of the time, to release the resources of each VCL component, Finally, the resources of the VCL array are only recovered.
##################
An array of VCL controls in BCB (2)
Hoist
In my "BCB uses VCL control array", it is impossible to release the resource when implemented with TLIST. As a result, I get answers today, Joy Yanhua and so on, to advise the release method of TLIST implementation, define the code as the following article:
For (int i = 1; i <= buttoncount; i )
{
TspeedButton * SPDBTN = New TspeedButton (this);
SPDBTN-> Parent = scrollbox; // Specify a parent control
SPDBTN-> CAPTION = INTOSTR (i);
SPDBTN-> Width = 80;
SPDBTN-> Height = 80;
SPDBTN-> OnClick = buttonclick;
SPDBTN-> LEFT = intLeft;
SPDBTN-> TOP = INTTOP;
SPDBTN-> GroupIndex = 1;
SPDBTN-> FLAT = true;
INTLEFT = INTLEFT 80 INTSPACE;
IF (i% linecount == 0)
{
INTTOP = INTTOP 80 INTSPACE;
INTLEFT = INTSPACE;
}
Buttons-> add (spdbtn); // Buttons is a TLIST pointer
}
The code that releases the resource is as follows:
INT Num = button-> count;
For (int i = 0; i { Delete (tspeedbutton *) Button-> items [i]; } In fact, it is to delete each item of TLIST, but because the TLIST-> Items type is Void *, in C / C , Void * can match any type, so just add a mandatory type conversion (tspeedbutton *). Of course, it is also possible to use (TOBJECT *), because TOBJECT is a base class in the VCL, and the pointer of the base class can point to its direct or indirect subclass. Comparison of two methods: I will be said to be a typical C solution, and this method can be said to be a solution of C Builder, which is more flexible and efficient with double pointers, but everyone knows that multiple pointers Not so good, I think I used to have used 4 heavy pointers (don't look at me!), I have already arrived. And this method is more understandable, and it is more convenient to use the TLIST class, but it is better to use it. In general, the two methods have the advantages and disadvantages. Specific use of that kind of person. The preference is. __________________