BCB master advancement (10) How to implement an array of controls

zhaozj2021-02-16  50

In C Builder, there is no need to provide a function of the array of controls in VB. After a dip, I finally solved this problem. Skills don't dare to enjoy exclusive, dedication for everyone to communicate.

In VB, the control arrays can:

Allows multiple controls to share the same event handle to provide a mechanism for adding a control during operation to provide a convenient combination control method.

The first two have been achieved in C Builder, and CB has a better advantage. That is, different types of controls can use the same handle (you only need to set up the EVENT event settings in the Object Inspector window of the relevant controls). The TLIST class object is used in C Builder to combine control arrays. The VB control array element must be compared to the same type of control. The TLIST object in C Builder can combine any type of control without strictness, so that it is greatly convenient. Procedure developer. For example, you can combine all controls on different Panel panel controls as an array of controls. In specific development practice, I use TLIST objects to create multiple types of controls arrays. Its practice and method see the original code code. The instance program implements the dynamically created an array of controls that contain 8 TEDIT type controls and 4 TIMAGE controls, modify the created controls in the program run. 1. Create a new project file (New Application), put two TPANEL types of Panel1 and Panel2 on Form1, adjust the size, then place four TButton type Button1, Button2, Button3, Button4, Settings control below the form The property is as follows: Button1-> CAPTION = "New Control Array", Button2-> CAPTION = "Changing the Control Location", Button3-> CAPTION = "Restore to the original position", Button4-> caption = "exit"; button2-> enabled = false, button3-> enabled = false. 2, add the following declaration in the file unit1.h: Class TForm1: public tform {public tpalic: // IDE-managed components TPANEL * PANEL1; TPANEL * Panel2; TButton * Button2; TButton * Button3; tbutton * button4; Private: // user declarations TList * myvcl; public: // user declarations __fastcall tform1 (tComponent * Owner); Virtual __fastcall ~ tform1 ();}; 3, switch to the project's FORM interface, double-click the main interface of the project, create A OnCreate event handle, add the following code in the file unit1.cpp: void __fastcall tform1 :: formcreate (TOBJECT * Sender) {myvcl = new tlist; // Create TLIST object} Add TFORM1 designer functions to file unit1.cpp : __Fastcall tform1 :: ~ tform1 () {delete myvcl; // Delete TLIST object} 4, double-click the tab to create a button of "Create Control Array", create an OnClick event handle, add the following code to the onclick event handle: Void __fastcall tform1 :: Button1click (TOBJECT * Sender) {// Create a new control, adjust its location, and add int temptop = 5; for (int i = 0; i <4; i ) )

{TEDIT * EditNow = New TEDIT (this); EditNow-> Parent = Panel1; EditNow-> Text = INTOSTR (i); ednow-> readonly = true; editnow-> top = temptop; editnow-> height = 24; editnow -> width = 24; EditNow-> LEFT = 10; MYVCL-> add (editnow); // Add to control array TIMAGE * imageoff = new timage (this); imageoff-> parent = panel1; imageoff-> Picture- > LoadFromFile ("none.bmp"); imageoff-> TOP = Temptop; Imageoff-> height = 24; imageoff-> width = 24; imageoff-> left = EditNow-> Left EditNow-> width; myvcl-> add (Imageoff); // Add to control array TEDIT * editstatus = new tedit (this); editstatus-> parent = panel1; editstatus-> font-> name = "arial"; editstatus-> font-> size = 12; EditStatus-> Text = "Forbidden"; EditStatus-> ReadOnly = true; EditStatus-> Top = temptop; EditStatus-> Height = 24; EditStatus-> Width = 80; EditStatus-> Left = ImageOff-> Left ImageOff- > Width; myvcl-> add (editstatus); // Add to control array temptop = temptop 24 5; button1-> enabled = false; button2-> enabled = true; } 5, the method shown in the same, double-click the title (CAPTION) to "change the control position", "Restore to the original position", "exit" button, create the corresponding ONCLICK event handle, add the following code to the corresponding ONCLICK event handle: void __fastcall tform1 :: button2click (Tobject * sender) {for (int i = 0; i <4; i ) ((TIMAGE *) MYVCL-> items [i * 3 1]) -> ParentT = Panel2; button2-> enabled = false; button3-> enabled = true;} void __fastcall tform1 :: button3click (Tobject * sender) {for (int i = 0; i <4; i ) ((TIMAGE *) MYVCL- > Items [i * 3

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

New Post(0)