[CB] array of controls

xiaoxiao2021-03-06  16

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 statement 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. Switch 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 a TLIST object

}

Add TFORM1 destructor to file unit1.cpp:

__fastcall tform1 :: ~ tform1 ()

{

Delete myvcl; // Delete TLIST object

}

4. Double-click the "CAPTION" button to create a button, 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 join the MYVCL (TLIST class)

INT TEMPTOP = 5;

For (int i = 0; i <4; i ) {

Tedit * EditNow = New Tedit (this);

EditNow-> Parent = panel1;

EditNow-> Text = INTOSTR (i);

EditNow-> 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 = eddomnow-> 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";

EditStatus-> Readonly = true;

EditStatus-> TOP = Temptop;

EditStatus-> height = 24;

EditStatus-> width = 80; edstatus-> 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 In the event handle:

Void __fastcall tform1 :: button2click (Tobject * 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 :: button4click (TOBJECT * Sender)

{

Close (); // Close the form

}

From the above, the actual code is mostly used to set the control position and basic attribute. The code that really implements an array function of the control is not much, not complicated, and is very flexible; need to pay attention to the use of TList class object combination Before the various controls, it must be forced to convert it into an object pointer to indicate its type to modify / assign its properties.

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

New Post(0)