Validate control data by limiting focus
The Validate event and the CausesValidation property are used in tandem to confirm input to a control before allowing the user to move focus away from the control. For example: Consider an application with several text boxes and a Help button. When each text box receives focus, you want to prevent the user from moving the focus until the text box's special validation criteria are met; however, you also want to allow the user to move the focus when the text box receives focus. The Help button can be clicked at any time. To do this, set the validation criteria in the Validate event and set the Help button's CausesValidation property to False. If the property is set to True (the default setting), the Validate event will occur on the first control. If the property is set to False, the Validate event on the first control will occur first.
The Validate event is more suitable for validating data entries than the LostFocus event because the LostFocus event (by definition) occurs after the focus has moved. Instead, by using the Validate event, you can prevent focus from moving to another control until the validation rules are met.
possible uses
Data entry applications need to perform more complex data entry validation than the validation provided by the MaskedEdit control or that occurs in business rules.
The form needs to prevent the user from using the TAB key or accelerator keys to move controls until data has been entered into the field.
ActiveX documents running in Internet Explorer need a way for the user to finish working on the form before the script moves focus programmatically.
Control focus on Validate event
The Validate event includes a keepfocus parameter. When the parameter is set to True, the control retains focus. This effectively prevents the user from clicking on other controls.
Using an array of controls
A control array is a group of controls with a common name and type. Their course of events is also the same. A control array should have at least one element, and the number of elements can be increased within the scope allowed by system resources and memory; the size of the array also depends on the memory and Windows resources required by each control. The maximum index value available in the control array is 32767. Elements in the same control array have their own property settings. Common uses for control arrays include implementing menu controls and option button groupings.
Note that Visual Basic includes the ability to dynamically add unreferenced controls to the Controls collection at run time. This topic only refers to reference controls that are added at design time by cutting and pasting a control onto a form. For more information about adding controls at run time, see the reference topics "Add method (Controls collection)" and "Add method (Licenses collection)".
Why use control arrays
At design time, adding controls using a control array consumes fewer resources than adding multiple controls of the same type directly to the form. Control arrays are also useful when you want several controls to share code. For example, if you create a control array containing three option buttons, the same code will be executed regardless of which button is clicked.
To create a new instance of a control at run time, the new control must be a member of the controls array. When using an array of controls, each new member inherits the array's public event procedures.
It is not possible to create new controls at runtime using the control array mechanism, because each new control inherits the event procedure written for the array. For example, if you have several text boxes on your form, and each text box accepts a date value, you can create an array of controls so that all text boxes share the same validation code.
Sample application: Calc.vbp
The Calculator sample application shown in Figure 7.2 (listed in the Samples!Alink(vbsamples) directory) contains two control arrays—numeric buttons and action buttons.
Notice how the example uses object(index) syntax to reference each control. Specify the index value when creating the control. In fact, specifying an arbitrary index for a control at design time will make the control part of an array.
The Index property distinguishes elements in the control array. When a control in the array recognizes an event, Visual Basic calls the public event procedure and passes a parameter (the value of the Index property) to identify which control recognized the event.
For example, the first line of code in the Number_Click event procedure looks like this:
PRivateSubNumber_Click(IndexAsInteger)
If Number(0) identifies the event, VisualBasic passes 0 as the index parameter, and if Number(1) identifies the event, VisualBasic passes 1 as the index parameter. Unlike the index value, the rest of the Number_Click code that has been executed is the same for Number(0) to Number(9).
Create control array at design time
There are three ways to create control arrays at design time:
1. Give the same name to multiple controls.
2. Copy the existing control and paste it on the form.
3. Set the Index property of the control to a non-Null value.
Note that the menu control array must be created in the Menu Editor. For more information about this operation, see "Creating and Modifying Menus at Run Time" in Chapter 6, "Creating the User Interface."
To add a control array element by changing the control name:
1. Draw the control to be added in the control array (must be the same type of control) to determine which control will be the first element in the array.
2. Select the control and change its Name setting value to the Name setting value of the first element of the array.
3. When you enter an existing name for a control in the array, Visual Basic will display a dialog box asking to confirm whether you want to create the control array. At this point select "OK" to confirm the operation.
For example, if the first element of the control array is named cmdCtlArr, select a CommandButton, add it to the array, and set its name to cmdCtlArr. At this time, the following message will be displayed: "A control named 'cmdCtlArr' already exists. . Do you want to create an array of controls?" Select OK to confirm the operation.
Controls added this way share only the Name property and control type; other properties have the same values as when the control was originally drawn.
To add a control array element by copying an existing control:
1. Draw the controls in the control array.
2. When the control gets focus, select the "Copy" command in the "Edit" menu.
3. In the "Edit" menu, select the "Paste" command. Visual Basic will display a dialog box asking whether to confirm creating the control array. Select OK to confirm the operation. The index value assigned to the control is 1. The first control drawn has index value 0.
The index value of each new array element is the same as the order in which it was added to the control array. When you add a control like this, most visual properties, such as height, width, and color, are copied from the first control in the array to the new control.
Add control array at runtime
At run time, you can use the Load and Unload statements to add and remove controls from the control array. However, the added control must be an element of an existing control array. You must create a control with (in most cases) an Index property of 0 at design time, and then use the following syntax at runtime:
When loading a new element of a control array, most property settings will be copied from the existing element in the array with the smallest subscript—in this case, the element with index 0. Because the Visible, Index, and TabIndex property settings are not automatically copied to new elements in the control array, in order to make the newly added control visible, its Visible property must be set to True.
Note that Visual Basic will generate an error when trying to use the Load statement on an index value that already exists in the array.
The important point is that the Unload statement can be used to delete all controls created by the Load statement. However, Unload cannot delete controls created at design time, regardless of whether they are part of the control array.
Control scheme: adding and removing controls in the control array
How to add and remove controls at run time is demonstrated in the control array example, where the control is an option button. According to this example, the user can add option buttons to change the background color of the picture box.
As shown in Figure 7.3, start the form, and then draw a picture box, a label, two option buttons and three command buttons on it.
Control array application events
Next, you must add event procedures for the option button and command button. Start the application after adding the form declaration:
DimMaxIdAsInteger
All option buttons share the Click event procedure:
PrivateSuboptButton_Click(IndexAsInteger)
picDisplay.BackColor=QBColor(Index 1)
EndSub
Add a new option button through the "Add" command button's Click event procedure. In this example, before executing the Load statement, the code checks to make sure that no more than ten option buttons are loaded. After the control is loaded, its Visible property must be set to True.
PrivateSubcmdAdd_Click()
IfMaxId=0ThenMaxId=1 'Set all option buttons.
IfMaxId>8ThenExitSub 'Only ten buttons are allowed.
MaxId=MaxId 1 'Button count increases.
LoadoptButton(MaxId) 'Create a new button.
optButton(0).SetFocus 'Reset button options.
'Place the new button below the previous button.
optButton(MaxId).Top=optButton(MaxId-1)._
Top 400
optButton(MaxId).Visible=True 'Show new button.
optButton(MaxId).Caption=Option&MaxId 1
EndSub
Delete an option button through the Delete command button's Click event procedure:
PrivateSubcmdDelete_Click()
IfMaxId<=1ThenExitSub 'Retain the first two buttons.
UnloadoptButton(MaxId) 'Delete the last button.
MaxId=MaxId-1 'Button count decreases.
optButton(0).SetFocus 'Reset button options.
EndSub
End the application through the Close button's Click event procedure:
PrivateSubcmdClose_Click()
UnloadMe
EndSub
->