VB's control Data

Source: Internet
Author: User
Tags array example arrays exit contains count integer reset

Validating control data by restricting focus
The Validate event and the CausesValidation property are used in tandem to confirm the input to the control before allowing the user to move the focus away from the control. For example, suppose you have several text boxes and a Help button application that, when each text box receives the focus, prevents the user from moving the focus until the special validation criteria for the text box is met; However, you also want to allow users to click the Help button 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 be preempted.
Validate events are more appropriate for validating data entry than LostFocus events, because the LostFocus event (as defined) occurs after the focus has been moved. Instead, by using the Validate event, you can prevent the focus from moving to another control until the validation rule is satisfied.
Possible use of
Data entry applications need to perform data entry validation that is more complex than that provided by the masked Edit control or that occurs in a business rule.
The form needs to prevent the user from using the TAB key or accelerator key to move the control until the data has been entered in the field.
An ActiveX document running in Internet Explorer requires a way for the user to complete the action on the form before the script moves the focus programmatically.

Controlling focus on the Validate event
The Validate event includes a keepfocus parameter. When the argument is set to true, the control retains the focus. This effectively prevents the user from clicking another control.

Using control arrays
A control array is a set of controls that have a common name and type. They also have the same event procedure. A control array should have at least one element, and the number of elements can be increased within the system resources and memory allowed; the size of the array also depends on the memory and Windows resources required for each control. The maximum index value available in the control array is 32767. Elements in the same control array have their own property setting values. Common uses of control arrays include implementing menu controls and grouping of option buttons.
Note Visual Basic includes the ability to dynamically add unreferenced controls to the Controls collection at run time. This topic refers only to reference controls that are added to a form at design time by cutting and pasting a control. For more information about adding controls at run time, see the reference topic "Add Method (Controls collection)" and "Add Method (Licenses collection)."

Why use a control array
At design time, using a control array to add a control consumes less resources than adding multiple controls of the same type directly to the form. An array of controls is also useful when you want several controls to share code. For example, if you create a control array that contains 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 control array. When you use a control array, each new member inherits the public event procedure for the array.
It is not possible to create a new control at run time using the control array mechanism, because each new control inherits an array of event procedures that are written well. For example, if you have several text boxes on a form, and each text box accepts a date value, you can create an array of controls so that all text boxes share the same legality check code.

Sample application: Calc.vbp
The calculator sample application, shown in Figure 7.2, is listed in the samples! The Alink ("Vbsamples") directory contains two control arrays-numeric buttons and action buttons.

Note how the example uses Object (index) syntax to reference each control. Specifies the index value when the control is created. In fact, you specify any index for a control at design time, which makes the control part of the array. The
Index property distinguishes elements in the control array. When one of the controls in the array recognizes an event, Visual Basic invokes the public event procedure and passes a parameter (the value of the Index property) to tell which control identifies the event.
For example, the first line of code for the Number_click event procedure is this:
Private Sub Number_click (Index as Integer)
if number (0) recognizes the event, the visual Basic passes 0 as the index parameter, and if number (1) Recognizes the event, Visual Basic passes 1 as the index parameter. Unlike index values, the remaining Number_click code that has been executed for number (0) to number (9) is the same.

Creating an array of controls at design time
There are three ways to create an array of controls at design time:
1. Assign the same name to multiple controls.
2. Copy the existing control and paste it onto the form.
3. Set the Index property of the control to a value other than Null.
Note You must create an array of menu controls in the menu editor. For more information about this operation, see chapter Sixth, "Create and modify menus at run time" in "Creating a user interface."
To add a control array element by changing the name of the control:
1. Draw the control in the array of controls that you want to add (must be a control of the same type) to determine which control acts as 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 in the array.
3. When you enter an existing name for a control in an array, Visual Basic displays a dialog box asking to confirm whether you want to create an array of controls. 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 to add it to the array and set its name to Cmdctlarr, and a message such as "There is already a control named ' Cmdctlarr" is displayed. Do you want to create a control array? ”。 Select OK to confirm the operation.
Controls added in this way only share the Name property and control type, and other properties are the same 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 has the focus, select the Copy command on the Edit menu.
3. On the Edit menu, select Paste. Visual Basic displays a dialog box asking whether you want to confirm the creation of the control array. Select OK to confirm the operation. The index value assigned to the control is 1. The first control drawn has an index value of 0.
The index value of each new array element is the same as the order it was added to the control array. When you add a control, most of the 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 run time
At run time, the controls in the control array can be added and deleted using the load and unload statements, however, the added control must be an element of an existing control array. You must create a control at design time (in most cases) with the Index property of 0, and then use the following syntax at run time:

When a new element of a control array is loaded, most property-setting values are copied by an existing element with the lowest subscript in the array-an element with an index value of 0 in this case. Because the visible, index, and TabIndex property setting values are not automatically copied to the new element of the control array, the Visible property must be set to true for the newly added control to be visible.
Note When you try to use the LOAD statement for an index value that already exists in an array, Visual basic generates an error.
Focus the Unload statement can be used to remove all controls created by the LOAD statement, however, unload cannot delete controls created at design time, whether they are part of a control array.

Control scenarios: Adding and removing controls in the control array
How to add and remove controls at run time, the control array example demonstrates this, where the control is an option button. Based on this example, the user can add an option button to change the background color of the picture frame.
As shown in Figure 7.3, start the form, and then draw a picture frame, a label, two option buttons, and three command buttons.

Event for control array application
Next, you must add an event procedure for option buttons and command buttons. To start an application after adding a form declaration:
Dim Maxid as Integer
All option buttons share the Click event procedure:
Private Sub Optbutton_click (Index as Integer)
Picdisplay.backcolor = QBColor (Index + 1)
End Sub
Add a new option button through the Click event procedure of the Add command button. In this example, before executing the LOAD statement, the code checks to make sure that no more than 10 option buttons are loaded. After the control is loaded, its Visible property must be set to true.
Private Sub Cmdadd_click ()
If maxid = 0 Then maxid = 1 ' Sets all option buttons.
If Maxid > 8 Then Exit Sub ' only allows 10 buttons.
Maxid = maxid + 1 ' button count increments.
Load Optbutton (MAXID) ' Creates a new button.
Optbutton (0). SetFocus ' reset button option.
' Place the New button below the previous button.
Optbutton (MAXID). top = Optbutton (MaxId-1). _
Top + 400
Optbutton (MAXID). Visible = True ' Displays the New button.
Optbutton (MAXID). Caption = "Option" & Maxid + 1
End Sub
To remove an option button from the Click event procedure of the Delete command button:
Private Sub Cmddelete_click ()
If maxid <= 1 Then Exit Sub ' preserves the original two buttons.
Unload Optbutton (MAXID) ' Deletes the last button.
Maxid = MaxId-1 ' button count decrements.
Optbutton (0). SetFocus ' reset button option.
End Sub
End the application with the Click event procedure of the Close button:
Private Sub Cmdclose_click ()
Unload Me
End Sub

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.