Implement the control array function in C ++ Builder

Source: Internet
Author: User
Borland C ++ builder is a new fast application development tool (RAD) launched by Inprise (formerly Borland ), the powerful C ++ language and quick and convenient visual programming are perfectly combined. Unfortunately, it does not directly provide functions like the control array in VB, and developers need to implement it themselves.
One control array in VB can be 1. Multiple controls are allowed to share the same event handle. 2. A control is added during running, 3. provides a convenient method for combining controls. The first two items have already been implemented 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 the event in the object Inspector window of the related control ).
In C ++ builder, The tlist class object is used to combine the control array. Compared with the VB control array element, the tlist class object must be of the same type, the tlist class objects in C ++ builder can combine any type of controls without forcing the same type, which greatly facilitates program developers. For example, you can combine all controls on different panel controls into a control array.
When developing a Real-time Monitoring Program, the author used the tlist class object to create and maintain a multi-type control array. For the implementation principle and method, see the original program code below. The instance program allows you to dynamically create an array of controls containing eight tedit controls and four timage controls, and modify and maintain the created controls during the running of the Program (for simplicity, only the parent attribute of the timage control is modified, and the Attribute Modification and event handling handle of each control in the control array can be modified in this way ).
1. Create a new project file (New Application), place two tpanel types of Panel1 and panel2 on form1, adjust the size appropriately, and then place four tbutton types of button1 under the form, button2, button3, and button4. Set the control attributes as follows: button1-> caption = "new control array", button2-> caption = "change control position ", button3-> caption = "Restore to original location", button4-> caption = "exit", button2-> enabled = false, button3-> enabled = false.
(Note: The following black text section contains the code to be manually added)
2. Add the following statement to the unit1.h file:
Class tform1: Public tform
{
_ Published: // ide-managed 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 form interface of the project, double-click the form interface of the project, create an oncreate event handle, and add the following code to the unit1.cpp file:
Void _ fastcall tform1: formcreate (tobject * sender)
{
Myvcl = new tlist; // create a tlist object
}
Add the tform1 destructor to the unit1.cpp file:
_ Fastcall tform1 ::~ Tform1 ()
{
Delete myvcl; // delete a tlist object
}
4. Double-click the "Create control array" button for the tag, create an onclick event handle, and add the following code to The onclick event handle:
Void _ fastcall tform1: button1click (tobject * sender)
{
// Create a new control, adjust its position, and add it to the myvcl (tlist class ).
Int temptop = 5;
For (INT I = 0; I <4; I ++ ){
Tedit * editnow = new tedit (this );
Editnow-> parent = Panel1;
Editnow-> text = inttostr (I );
Editnow-> readonly = true;
Editnow-> Top = temptop;
Editnow-> Height = 24;
Editnow-> width = 24;
Editnow-> left = 10;
Myvcl-> Add (editnow); // Add it to the 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 it to the control array
Tedit * editstatus = new tedit (this );
Editstatus-> parent = Panel1;
Editstatus-> font-> name = "Arial ";
Editstatus-> font-> size = 12;
Editstatus-> text = "Access prohibited ";
Editstatus-> readonly = true;
Editstatus-> Top = temptop;
Editstatus-> Height = 24;
Editstatus-> width = 80;
Editstatus-> left = imageoff-> left + imageoff-> width;
Myvcl-> Add (editstatus); // Add it to the control array
Temptop = temptop + 24 + 5;
}
Button1-> enabled = false;
Button2-> enabled = true;
}
5. Double-click the label title (Caption) as the "change control location", "Restore to original location", and "exit" button in the same way as 4, create an onclick event handle and add the following code to The onclick 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
}
As mentioned above, most of the actual code is only used to set the control location and basic attributes. The code that truly implements the control array function is not too much, and is not complex and flexible; before using the controls in the tlist Class Object combination, you must forcibly convert them into an object pointer to specify its type before you can modify or assign values to its properties.
The program in this article is debugged in C ++ builder 3.0/pwin95, C ++ Builder 4.0/pwin98.
Related Article

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.