VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 4

Adding and Deleting Controls Dynamically Using the Controls Collection in Visual Basic 6

The Add and Remove methods of the Controls Collection are new to VB6. You can use these methods to add and delete controls from a form instead of using the control array technique described in the previous section.

Following is an overview of the general steps that you need to take in order to dynamically add and remove controls with the Controls Collection (more detailed discussion is given in the following sections):

  1. Find out the control's ProgID, a unique string used by the Windows operating system (and stored in the Windows registry) for identifying the control's type.

  2. If the control is an intrinsic VB control, declare an object variable of the appropriate control type using WithEvents and program the resulting object's event procedures. If the control is an intrinsic VB control, then ignore steps 3, 6, and 7 that only apply to non-intrinsic controls.

  3. If the control is an ActiveX control (i.e., not an intrinsic VB control) then you must declare the type of its object variable as VBObjectExtender and place code in its ObjectEvent procedure to trap for the various events that you're interested in.

  4. Use the Add method of the Controls Collection to initialize the control with the ProgID that you determined in step 1, and set the result of the method to the object variable you declared in step 2 or 3. Set the control's Visible property to True and set any other properties that need to be changed. If the control is an ActiveX control, you'll need to refer to its members through the Object property of the control object variable.

  5. Use the Remove method of the Controls Collection to remove the control from the Controls Collection when your program is finished using the control.

  6. If an ActiveX control is in the Toolbox but is not otherwise referenced in your project with a design time instance on the surface of a form, then you must make sure that your project's properties are set appropriately to allow information about unused ActiveX controls to remain in the project.

  7. If an ActiveX control requires a license, then you must detect the control's license ID in your design time test environment and use that license ID to initialize the control in the compiled application that you distribute to end users. In order to do this legally, you must be licensed to use and distribute this control.

We discuss these steps in the following sections.

Related Topics


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved