Implementing dynamic creation components in BCB

Source: Internet
Author: User
Tags data structures

Stack is a piece of memory that holds all the dynamic local variables of the function and the function calls and the information returned. Stack memory management strictly follows the advanced order, which is what is needed to implement function calls. allocating memory from the stack is highly efficient. Data objects use memory in the stack, such as dynamic local variables, to make the program run faster than using memory in the heap.

A heap (heap) is a piece of memory for malloc (), Calloc (), realloc (), and new functions to obtain memory space. Getting memory from the heap is much slower than from the stack, but the memory management of the heap is much more flexible than the stack, and you can get (or release) memory from the heap at any time, and we can do it in any order. The memory used to hold recursive data structures is almost always fetched from the heap. The memory used to hold the strings is usually also fetched from the heap, especially for the long strings that may appear when the program is run.

The memory obtained from the heap is freed with free (), delete, and it is not automatically released.

C compiled programs can produce such high-quality code, the fast operation of the program is related to the proper use of the stack, but all objects in Object Pascal can only be constructed in the heap, not as C + +, can be in the stack (create the object of the class within the function), the data section (the object that creates the class outside the function), Heap (with new functions to create objects of the class) three kinds of places to set up objects, so objects of the VCL class can only be created in the heap.

If you create a button object, we can create it like this:

TButton *btnmy= New TButton (FROM1);

You can write the following program: Class name * Object name =new class name (...) );

Note: () it can be the parent name of the object you have created, the name of the project, NULL, or this. However, it is best to have the parent class name of the object.

Example: Dynamically generating buttons

Let's start with a button on the form (FORM1) Button1 and write the following code in his click event:

void __fastcall Tform1::button1click (tobject *sender)
{
TButton *my=new TButton (FORM1);
my->parent=form1;//The most critical sentence, otherwise you will not see anything, but the compilation is correct
my->top=200;
my->left=200;
my->height=25;
my->width=75;
my->caption= "I ' m button!";
my->visible=true; This statement is optional because his parent class usually defaults to his visible
}

In this example, we should clearly see several important steps for dynamically creating components:

1) to a space (memory);//TButton *my=new TButton (FORM1);

2 specifies its parent component, which means that the object we are creating should be placed on that container;//my->parent=form1;

3 Specifies the location where the component will appear in the parent class;//my->top=200;my->left=200;my->height=25; my->width=75; so this attribute you must set a good position;

4 other important attributes. my->caption= "I ' m button!";

And the order can not be reversed, or your program will be a joke.

The corresponding header file is added when dynamically generating components that are not Baolan VCL. For example, we want to dynamically generate report components to join:

#include "qrctrls.hpp"///If you have any questions, you will add:

#include "quickrpt.hpp"

Also because of BCB to memory management or with systems, hardware conflicts, your dynamic creation program may not be a mistake, but is not compiled; sometimes it may be the first time passed, the second time the same program is not passed, there is such a hint, the easiest way is to log off the system, and try again, Most of them can be solved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.