C ++ Builder dynamically creates controls (Tutorial) and builder controls

Source: Internet
Author: User

C ++ Builder dynamically creates controls (Tutorial) and builder controls

When developing an application similar to an interpreter or implementing a visual operation function, you must dynamically create controls (objects) and add appropriate event handlers to these controls, the following describes how to implement the basic implementation framework in C ++ Builder.

The general process of dynamically creating controls:

TButton * btn = new TButton (this );

Btn-> Caption = "Button1 ";

Btn-> Parent = this;

Btn-> Show ();

The following uses an example of dynamically generated menus to create controls dynamically and process add events.

First, write a function to insert a menu item to an existing menu object:

Void _ fastcall TForm1: AddContextMenuItem (TPopupMenu * pMenu,

AnsiString caption, unsigned int tag, tpolicyevent policyevent)

Parameters:

PMenu: an existing TPopupMenu object pointer

Caption: the title of the menu item

Tag: The tag value of a menu item. It is used to identify different TMenuItem objects.

Policyevent: The event processing function of this menu item.

Function implementation:

TMenuItem * mnuItem = new TMenuItem (pMenu); // Create new item

PMenu-> Items-> Add (mnuItem); // Add it to pMenu

MnuItem-> Name = "CM _" + IntToStr (tag );

MnuItem-> Caption = caption;

MnuItem-> ImageIndex = imgIndex;

MnuItem-> Tag = tag;

MnuItem-> OnClick = policyevent; // Assign it an event handler

Then, compile the CreateContextMenu () function that calls AddContextMenuItem (...) and define it as follows:

Void _ fastcall TForm1: CreateContextMenu (void)

{

FTagInc = 0;

FContextMenu = new TPopupMenu (this );

FContextMenu-> AutoHotkeys = maManual;

FContextMenu-> OnPopup = UpdateContextMenuItem;

AddContextMenuItem (FContextMenu, "menu item 1", FTagInc ++, MenuItemClick );

AddContextMenuItem (FContextMenu, "menu item 2", FTagInc ++, MenuItemClick );

AddContextMenuItem (FContextMenu, "-", FTagInc ++, nullpolicyevent );

AddContextMenuItem (FContextMenu, "menu item 3", FTagInc ++, MenuItemClick );

}

FContextMenu is a member variable of TForm1, and UpdateContextMenuItem () is used to update menu items in the Popup event of FContextMenu.

Both MenuItemClick and nullpolicyevent are member functions in TForm1. They are the event processing functions of dynamically created controls. The format is:

Void _ fastcall TForm1: MenuItemClick (TObject * Sender)

Void _ fastcall TForm1: nullpolicyevent (TObject * Sender)

Bytes ---------------------------------------------------------------------------------------------

The Code is as follows:

. H file:

Class TMyThread: public TThread

{

Public:

_ Fastcall TMyThread (void );

Private:

Void _ fastcall Execute ();

};

In the. cpp file:

_ Fastcall TMyThread: TMyThread (void): TThread (true)

{

FreeOnTerminate = true;

Resume ();

}

Void _ fastcall TMyThread: Execute ()

{

Form1-> createview ();

}

Void _ fastcall TForm1: Button1Click (TObject * Sender)

{

TMyThread * mythread = new TMyThread;

}

Void _ fastcall TForm1: createview () // createview () is a function defined by myself.

{

// Dynamically generate the LISTVIEW list

TListView * tempview = new TListView (Form1 );

Tempview-> Visible = true;

Tempview-> Parent = Form1;

}

There are two problems after the above Code is run:

First, the listview control is not created when you click the Button.

Second, the following error message is displayed when you close the application:

An unknown software exception occurs in the application (0x0eedfade) at 0x77e8bc3f.

1:

Void _ fastcall TForm1: Button1Click (TObject * Sender)

{

TMyThread * mythread = new TMyThread ();

Mythread-> Execute;

}

2: It is estimated that the memory has not been released !!

TO: caizhen2000_82 (great brother !!!)

Your method is not good, mythread-> Execute; there is no such method at all.

Is the memory not released? Can we say something specific?

I hope you can provide feasible solutions online!

Form1-> createview (); Form1 ??

Where do you want to put the ListView?

Put it on form1.

_ Fastcall TMyThread: TMyThread (void): TThread (true)

{

TListView * ListView1 = new TListView (Form1 );

ListView1-> Parent = Form1

FreeOnTerminate = true;

Resume ();

}

Put in the constructor.

TO: chpst (Dipper)

Thank you very much for your help. We have solved all the two problems, but there are new problems!

How can I call a dynamically generated listview IN THE createview () function?

Bytes ---------------------------------------------------------------------------------------------

Dynamic Control Creation

1. button. How to control its position after dynamic creation?

2. This control should be placed on another control, such as panel. How can this control be written?

3. If the name of the dynamically created button is dynamic, how can I write it? For example, first determine whether to create a button. If you need to create a button1, then determine, and then create button2. In this case, how to name the button?

Fjye (Ginger) on the first floor)

TButton *;

A = new TButton (this );

A-> Parent = Form1;

A-> Top = 10;

A-> Left = 10;

A-> Width = 30;

A-> Height = 10;

A-> Caption = "haha ";

If you want to put it on the Panel, the specified Parent indicates the Panel.

Fjye (Ginger) on the second floor)

I personally prefer to use control Arrays for dynamic names.

For example

TButton * button [10];

The created method is the same.

Button [0] = new TButton (this );

You can define a global variable num instead of the Top variable position in square brackets.

JUNE20 on the third floor (huafang: I want to learn BCB !)

TButton * TempButton = new TButton (this );

TempButton-> Parent = Form1;

TempButton-> Top = 00;

TempButton-> Left = 00;

... Width = 00;

... Height = 00;

... Caption = 00;

TempButton-> Show ();

Top

Xiaoshi0 (Rain) on the 4th floor)

The dynamically created control array does not need a name. To create a control array on a Panel, you only need to set the Parent attribute of the control to Panel.

ToIP (zhuimu) on the fifth floor)

//..........

Buttone1-> Update (); Top

6 floor wccpc3 (pig Yiyi)

TButton *;

String = buttonname = "button" + I; // I is defined as a global variable.

A = new TButton (this );

A-> Parent = Panel;

A-> Top = 10;

A-> Left = 10;

A-> Width = 30;

A-> Height = 10;

A-> Name = buttonname;

Note: I don't know if this is the name of the sub-row. If the system does not allow the Top

7-floor wccpc3 (pig Yiyi)

Do not forget I + 1 below

Bytes ------------------------------------------------------------------------------------

In some cases, due to special requirements, You need to develop your own VCL components to meet your needs. Some questions should be emphasized for beginners.

1: naming of files and Classes

Menu operation process: "Component-> New Component... ", enter your class name in the" Unit file name "column of the" New Component "dialog box. If your class name is" TMyClass ", enter" MyClass "here ", note that "T" is not added here ". The file will be opened after confirmation.

After editing and saving the source file, Install the Component in the IDE environment as follows: "Component-> Install Component... ", select the" Into new package "page, and enter the path and file name of the component source file to be installed in" Unit file name, enter the path and file name of the new Package file to be generated in "Package file name". Note that the Package name in you should be the class name, for example, "TMyClass. bpk ", this will generate. bpk package file and package source file "TMyClass. cpp ", if you write the package name to MyClass. bpk, then the source file of your component will be overwritten. Of course, you can choose to store it in different paths, but this will not be easy to manage and cause confusion.

Second: How to customize icons for new components

Generally, you need to select an appropriate icon for the component you have compiled to better express the function of the component, this icon is displayed in the component bar of the RAD environment during the design stage. The size is generally 24x24.

First, open the Image Editor provided by C ++ Builder and select "File-> New... -> Component Resource File (. dcr) ", select" Resource-> Bitmap ", enter the size and color data, and then create a Bitmap for you to edit. The default Resource name is" Bitmap1 ", you should change the name to your. bpl file names are consistent, such as those of your component. bpl is "TMyComponent. bpl, the name should be "TMYCOMPONENT", and the combination is capitalized. Double-click the node to edit your icon. You can also use PhotoShop or other software to paste the icon directly by using the "Paste" command. Note that up to 256 colors are supported.

When the icon painting is complete, you can save the disk. You must pay attention to the naming problem. The file name must be consistent with your component class name. For example, your component class name is "MyComponent ", the file name should be "MYCOMPONENT. dcr, and all uppercase letters. After installing your component again, you can use your custom icon.

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.