Link to Us

Used for mainly for entering text
Enter -       when user clicks on or tabs into text control
Leave        -       user chooses another control
Validated - when control finishes validating
Validating -         before control validates control
Textchanges -     when text has changed

Lets you enter text with keyboard
Supports backspacing, deleting, navigating with cursor keys
Can cut, copy and paste using Clipboard
You can also select part or all of text

causesValidation -        true/false if raises validating event
lines-                         string array providing access to lines in control
if multiline
passwordChar -           character to use for password
multiline                     true/false
wordwrap                   true/false
text                           text inside control
selectedText               text user selected
selectionStart              beginning of selection
selectionLength           number of characters user selected



  • start new project
  • add textbox and button
  • double-click the button and add this code:

If TextBox1.SelectionLength = 0 Then
MessageBox.Show("Select some text")
MessageBox.Show(TextBox1.SelectedText & " was selected")
End If
multiline textboxes
MultiLine textboxes

  • Start a new windows project
  • Drop textbox on form
  • Resize form
  • Resize textbox to take up most of the form
  • To do this change multiline property to true
  • Rename textbox to txtMultiline
  • Drop a button below it
  • Change text on the button to showLines

show lines

  • Double click the button
  • Add this code

Dim linedata As New System.Text.StringBuilder()
For Each line As String In txtmultiline.lines
linedata.Append(line + vbCrLf)

vbCrLf = new line
validating textboxes
Validating textbox

The validation event fires after the user changes focus away from the textbox
This can be done by selecting a different control or closing the form

Validated event occurs after the validating event is done unless it is cancelled
example validating textboxes
Example- Validating textbox control

  • Start a new windows project
  • Lay out controls as follows

enter a number from 1 to 20

    • Label
    • Textbox
    • Button

You want to check that the user entered a number between 1 and 20 and not text

  • Double-click button and add this code:

This Closes the app

  • Doubleclick validating event on the textbox in property window
  • Add this code

Dim num As Integer
If Integer.TryParse(TextBox1.Text, num) Then
If num < 1 Or num > 20 Then
e.Cancel = True
MessageBox.Show("Enter from 1 to 20")
End If
e.Cancel = True
MessageBox.Show("You need an integer")
End If

tryparse() will take the first parameter and put it in the second parameter
if its not a number tryparse returns false

look at the signature
Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating

Inside the e object is a property called cancel
This takes a true or false
You must set this to true if validation fails

You are canceling whatever triggered the event

If e.cancel is false then validation passed

  • Add this code to the validated event


MessageBox.Show("Good job You did it")
masked textbox control
MaskedTextbox Control
Special kind of textbox
Can limit the data a user enters and formats it automatically
You can auto format it with thousands separators
Make correct length

Mask property determines what the user can and cannot enter into the control
formats it to 2 numbers. Dash, 2 numbers, dash and 2 numbers

there are also a number of built in masks you can access from the smart tag
set mask in masked textbox control in visual basic
choose set mask from the menu

select a mask
mask shows actual value entered in mask for you
preview shows a preview of the mask as the user will see it
input mask dialog box

allows you to define a pattern to accept user input

Mask-Main Property - the input mask
Textmaskformat-if prompt characters and literal characters are included in the text returned by text property
AsciiOnly-boolean-if true only A-Z and a-z

Mask property
Elements of Default MaskedTextprovider


Required 0-9


Optional 0-9


Optional 0-9, or space, or + or -


Required Letter A-z or a-z


Optional letter A-Z or a-z


Required character


Optional character


Optional alphanumeric character


Decimal character


Thousands placeholder


Time separator


Date Separator


Currency Symbol

Converts all characters after this to lowercase

Converts all characters after this to uppercase


Disables previous shift up or down


Escape a mask character. Example: \\ is a backslash

All other characters

Appear as themselves

Prompt character is an underscore by default 

textbox control