Visual Basic Tutorials – Controls – TextBox Control

The TextBox Control

The TextBox controlis the primary mechanism for displaying and entering text. It is a small text editor that provides all the basic text-editing facilities: inserting and selecting text, scrolling if the text doesn’t fit in the control’s area, and even exchanging text with other applications through the Clipboard.

Textbox control in Visual Basic

Textbox control in Visual Basic

The Textbox control is one of the most versatile tools in the Visual Basic toolbox. This control performs two functions:

  • Displaying output (such as operating instructions or the contents of a file) on a form.
  • Receiving text (such as names and phone numbers) as user input.

How a text box works depends on how you set its properties and how you reference the text box in your program code. The following pop-up illustration shows a text box that:

  • Displays data entry instructions for the user.
  • Receives input when the user types in a delivery note and clicks the Print Order button.

Basic Properties: TextBox

The properties special to the text box control are:

Appearance Selects 3-D or flat appearance.
BorderStyle Determines type of border.
Font Sets font type, style, size
MaxLength Limits the length of displayed text (0 value indicates unlimited length)
MultiLine Specifies whether text box displays single line or multiple lines.
PasswordChar Hides text with a single character
ScrollBars Specifies type of displayed scroll bar(s).
SelLength Length of selected text (run-time only)
SelStart Starting position of selected text (runtime only)
SetText Selected Text(run-time only)
Tag Stores a string expression
Text Displayed Text

 Events: TextBox

TextBox control is used for defining event procedures in visual programming during application development. The often used text box events are:

Change Triggered every time the Text property changes.
LostFocus Triggered when the user leaves the text box.  This is a good place to examine the contents of a text box after editing.
KeyPress Triggered whenever a key is pressed.  Used for key trapping, as seen in last class.
Click Occurs whenever user clicks in the text box

Text Box Methods:

Some Methods of textbox are:

SetFocus Places the cursor in a specified text box.
Move  Moves the textbox
Drag  Drags the textbox around the form

Creating a Password Control TextBox

It’s time to heighten security. Users of your new Super Special DataBase program are worried about the low security of your program, so you decide to add a little security with password controls. Visual Basic will help out.

Creating password control in TextBox Visual basic

Creating password control in TextBox Visual basic

To convert a standard text box into a password box, you just assign some character (usually an asterisk [*]) to the text box’s PasswordChar property. After that, your program can read the text in the text box, but only the password character will appear on the screen each time the user types a character, as shown in Figure.

Program Statement for login with the textbox that accepts password from the user:


Private Sub cmdLogin_Click()

If txtPassword.Text = "admin" Then

    Unload Me



    MsgBox "Invalid Password, Try Again!", , "Login"

    txtPassword.Text = ""


End If

End Sub


TIPS: You may be concerned that someone can copy the text in a password control and paste it into a word processor to read it, but in fact, Clipboard-handling from the text box is disabled if you are using a password character.