Tuesday, September 22, 2009

Tips and Tricks Visual Basic 6.0 Force The User To Enter Valid Value In Text Box


Using the Validate event, you can assure that the user will enter valid value in the text box. "Valid Value" can be anything you define: Phone Number, date, number in certain range, certain strings, and so.
Until he done so, all the other controls are disabled, and he cannot exit the text box.
In this example, the user will not be able to exit the first Text Box, until he type "abc".
To check this code, try to press the button or enter to the second text box.


  Preparations

Add 1 Command Button and two Text Boxes to your form.

  Form Code


Private Sub Text1_Validate(Cancel As Boolean)
      Cancel = Text1.Text <> "abc"
End Sub

0 comments:

Your Ad Here