Occasionally you will create forms for which you do not always have a fixed number of controls. You may need to add controls at runtime or remove some of the controls that you have created. Visual Basic lets you create and destroy controls dynamically at runtime based on the needs of your application.
With VB6, you have at your disposal two major techniques for dynamic control creation:
*
Control arrays
*
Direct manipulation of the Controls Collection through the Add and Remove methods.
We discuss these techniques in the following two sections.
Adding and Deleting Controls Dynamically Using Control Arrays:
You must follow several rules to be able to create and remove controls with control arrays.
First you must have a control of the type that you will be adding placed on the desired form at runtime. If you will be adding TextBox controls to a form, for example, you must have at least one text box drawn on that form at design time. That control can be invisible, and there are no restrictions for the size or placement of that control, but it must be on the form at design time. This control will be the template for text boxes that you will add at runtime.
The second requirement for using dynamic control arrays is that the template object that you draw at design time must be part of a control array. Usually it is the only control of its type with its Index property set to a value of 0. Continuing with the text box example, you can have a form with only one text box as a template, and that text box must have its Index property set to some integer value (typically 0 or 1). If your application required it, you might have additional text boxes with Index values of 1, 2, 3, and so on.
As long as you have a control array with at least one object in it, you can create additional instances of that object dynamically at runtime.
After you have built a form with a control that has its Index property set, you can add additional controls to the control array at runtime. Assume, for example, that you have an application with one form, Form1, and one text box, Text1. Text1 has an Index value of 0. At runtime, you can create additional instances of Text1 on your form with code such as:
Load Text1 (index)
Method2:
Adding and Deleting Controls Dynamically Using the Controls Collection
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment