function of implementing control array in C++builder

Source: Internet
Author: User
Tags exit

Borland C + + Builder as the new Fast Application development tool (RAD) of the Inprise (formerly Borland) company, with the powerful C + + language and fast and convenient visual programming the advantages of the perfect combination, Unfortunately, it does not directly provide a function like VB in the control array, the developers need to implement their own programming.

An array of controls in VB can

    1. Allow multiple controls to share the same event handle
    2. Provides a mechanism for adding a control during run time
    3. Provides a convenient way to combine controls.

The first two are already implemented in C + + Builder, and CB has an advantage. That is, different types of controls can use the same handle (just the event event settings in the Object Inspector window of the related control).

C + + Builder uses the Tlist class object to combine the control array, and the VB control array element must be compared to the same type of control, the Tlist class object in C + + Builder can combine any type of control without forcing the same kind, thus greatly facilitates the program developer. For example, you can combine all the controls on different panel controls into one control array.

When developing the real-time monitoring program, the author applies the Tlist class object to create and maintain the multiple types of control arrays. The principle and method of its implementation are described in the following original program code. The instance program implements the dynamic creation of a control array containing 8 Tedit type controls and 4 Timage type controls, modifying and maintaining the controls that are created in the program run (for simplicity, only the parent property of the Timage control is modified, Property modification and event handling handles for each control in the control array can be modified in this way.

1, create a new engineering document (new application), place two tpanel types of Panel1 and Panel2 on the Form1, resize appropriately, and place four TButton type button1,button2 below the form. Button3,button4, set the control properties 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.

2, in the document Unit1.H add the following statement:

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 Project form interface, double-click the project's main interface form, create a OnCreate event handle, in the file Unit1.CPP add the following code:

void __fastcall tform1::formcreate (tobject *sender)
{
MYVCL = new tlist;//Create Tlist object
}
to TF Orm1 destructor added to file Unit1.CPP:
__fastcall tform1::~tform1 ()
{
Delete myvcl;//Delete Tlist object
}
4, double-click label (C aption) for the "Create Control array" 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 position, and add 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 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= "No access";
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 same as 4 of the method, double-click the label's title (Caption) for the "Change control position", "restore to 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])->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(); //关闭窗体
   }

As described above, the actual code is mostly just for setting control position and basic properties. The code that really implements the array of controls is not too much, not complex, and very flexible; it is important to note that before you use the controls in the Tlist class object group, you must cast them to an object pointer to indicate their type. Can modify/Assign operations on its properties.

This program is passed in the C + + Builder 3.0/pwin95,c++ Builder 4.0/pwin98 debugging.

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.