VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

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

Enabling Controls Based on Input in Visual Basic 6

A data entry screen often requires some of its controls to be selectively enabled, depending on the state of the rest of the data on the screen. Some examples of such selectively enabled controls might be:
  • A drop-down list of credit card types enabled only if the user selects the option button for "credit card" from the option button group for type of payment.

  • An OK button. This button signals that the user is finished with data input and that the data can be processed. The button should only be enabled if the data on all other fields is complete and valid.

In the first example above, the control that we wish to enable depends on just one other control or group of controls to be enabled. In the case of the example, it would be sufficient to put a line of code enabling or disabling the ListBox in the Click event of each type of payment option button as in Listing 5.5


Private Sub optCash_Click()
lstCreditCard.Enabled = False
End Sub
Private Sub optCreditCard_Click()
lstCreditCard.Enabled = True
End Sub
Private Sub optDebitCard_Click()
lstCreditCard.Enabled = False
End Sub

If the control to be enabled is a TextBox, you have the choice of two properties for disabling user input: the Enabled property and the Locked property. The two properties can be compared as follows:

  • The Enabled property is True by default. When Enabled is False, the user can't set focus to the control and the control or its contents appear grayed out to the user.

  • The Locked property (TextBoxes only) is False by default. When Locked is True, the user can still set focus to the control but can't make changes. The contents don't appear grayed out and the user can scroll through the contents if they are larger than the area of the TextBox. The Locked property is useful when you can't predict how big the contents of the TextBox will be, and you need to prevent user input but still want the user to be able to view all the contents.

In the example of the OK button, however, whether or not the button should be enabled depends on the validity of all the other fields on the screen. In some cases, it might depend on one or more relations between those fields. For instance, if "credit card" is chosen as the form of payment, then "credit card type" must not be left undefined. In order to allow your application to decide whether or not to enable an OK button, you could follow these steps:

  1. Write a Function procedure that performs all necessary data validation checks for the screen and returns a True or False result

  2. Call this Function from all event procedures where data changes (such as, Change event procedures for TextBoxes and Click event procedures for OptionButtons, CheckBoxes, and List and Combo boxes), and set the OK button's Enabled property in accordance with the return value of the Function.

See Also


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved