The function of implementing an array of controls in C ++ Builder

zhaozj2021-02-16  48

Borland C Builder As INPRISE (original Borland) New Fast Application Development Tool (RAD), it has the advantages of perfectly combined with powerful C language and fast and convenient visual programming programming, and unfortunately it does not directly provide The function of the array of controls requires developers to program their own. One control array in VB can be 1, allowing multiple controls to share the same event handle, 2, providing a mechanism for adding a control during operation, 3, providing 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. When developing a real-time monitoring program, use the TLIST class object to maintain multiple types of control 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 control in the program run (simple start, only for the Parent attribute of the TIMAGE control Modifications, you can modify the properties modifications and event handlers of each control in the control array. 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.

(Note: The following black body part is a code that needs to be manually added) 2, add the following declaration in the file unit1.h: Class TFORM1: Public TForm {__published: // IDE-management Components TPANEL * PANEL1; TPANEL * PANEL2; TBUTTON * button1; TButton * Button2; TButton * Button3; TButton * Button4; private: // User declarations TList * MyVCL; public: // User declarations __fastcall TForm1 (TComponent * Owner); virtual __fastcall ~ TForm1 ();}; 3, switching Go to the project's FORM interface, double-click the main interface form, create an 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 destructor to file unit1.cpp: __fastcall tform1 :: ~ tform1 () {delete myvcl; // Delete TLIST object} 4, double-click the tab to create a button, create a button, create a button A 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 it in MyVCL (TLIST class) int temptop = 5; for (int i = 0; i <4; i ) {TEDIT * EditNow = New Tedit (this); editnow-> parent = panel1; eddom1; edtnow-> text = INTSTR (i); editnow-> readonly = true; EditNow-> TOP = Temptop; EditNow-> Heig HT = 24; eddomnow-> width = 24; eddomnow-> 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 access "; edstatus-> readonly = true; editstatus-> top = Temptop; EditStatus-> height = 24; edctionsTatus-> width = 80; EditStatus-> Left = ImageOff-> Left ImageOff-> Width; MyVCL-> Add (EditStatus); // added to the control array temptop = temptop 24 5;} Button1-> Enabled = false; Button2-> Enabled = true;} 5, Similarly, double-click the title (CAPTION) of the label 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 Middle: void __fastcall tform1 :: butt * sender {for (int i = 0; i <4; i ) ((TIMAGE *) MYVCL-> items [i * 3 1]) -> Parent = 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 1]) -> parent = panel1; button3-> enabled = false; button2-> enabled = true;} void __fastcall tform1 :: butto N4Click (TOBJECT * Sender) {close (); // Turns the form}, the actual code is mostly used to set the control position and the basic attribute, and the code that really implements the array of controls is not much, also Not complicated, and very flexible; need to note that before using the various controls in the TLIST class object, you must force it to a target pointer to specify its type.

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

New Post(0)