How to Limit a Text Box to Numbers Only in Visual Basic 6.0


Text boxes are primarily used to receive input from users, or to display text either pre-programmed or from a database source. In Visual Basic 6.0, the "TextBox" control is used to draw a text box into a Windows Form. The control's properties can be set depending on the user requirements. For example, you can set a text box to read-only or editable, with different background color, or with scroll bars. It is also possible to limit what type of text input it can accept. Learn how to create a program in Visual Basic 6.0 that limits a text box input to numbers only.

  • Load the Visual Basic 6.0 environment by clicking the program's shortcut icon from the desktop, or selecting "Microsoft Visual Basic 6.0" from the "Start" menu in your computer.

  • Select "Standard EXE" from the "New Project" dialog box, then click "Open" button.

  • Add a text box into the form by clicking the "TextBox" control from the "Toolbox," then drawing the desired text box size. Make sure it's wide enough to hold input characters from the user.

  • Clear the text caption by erasing the word "Text1" next to the "Text" property at the "Properties" window on the right side of the screen.

  • Double-click the text box control to display the "Code" window. This is where you'll write the instructions or source codes that the program will execute at run-time. The source code you are going to write will validate user input and accepts only numeric characters, else it will display an error message.

  • Insert the following code within the "Text1_Change" event:

    If Not IsNumeric(Text1.Text) Then

    MsgBox "Numeric input only!", vbCritical, "Invalid Input"

    Text1.Text = ""

    End If

  • Press F5 or select "Run" then "Start" from the menu to switch to runtime-mode.

  • Test the program to check that it's running as it should. Type alphanumeric characters or combination of letters and numbers into the text box. You should see an error message letting you know the input is invalid. Now, type only numbers into the text box. If no error message pops up, the program is running correctly.

  • Save the program by selecting "Save Project" from the "File" menu. Provide a filename into the space provided, then click "Save."

Related Searches


  • Photo Credit Jupiterimages/Polka Dot/Getty Images
Promoted By Zergnet


You May Also Like

Related Searches

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!