MaskedTextBox control in VB.NET
In this article we demonstrate on what is MaskedTextBox,its properties and how can you use this MaskedTextBox control.
A visual basic MaskedTextBox control provides validation mechanism for user input on a form. It is the best way to stop users from entering invalid data into an input control. The MaskedTextBox control constrains the format of user input by distinguishing between proper and improper user input. The Mask property of the MaskedTextBox control allow you to specify whether input characters are letters or numbers, as well as any formatting.
Properties of MaskedTextBox control:-
- AccessibilityObject:- Determines the AccessibilityObject assigned to the control.
- AccessibleRole:- Determines the accessible Role of the control.
- AutoScrollOffset:- Determines where this control Scrolled to in ScrollControlIntoView.
- BackColor:- Determines the background color of the control.
- BindingContext:- Determines BindingContext of the control.
How to use MaskedTextBox Control
- Open a new project.
- Drag two MaskedTextBox, Label, Buttons on form. Form will display like below.
-
Change the class name to frmMain and write the below code.
Private Sub mtbMaskTest_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles mtbMaskTest.GotFocus
'Set mask in test control
mtbMaskTest.Mask = txtMask.Text
'Select all to ease data entry
mtbMaskTest.SelectAll()
End Sub
Private Sub btnTestMask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestMask.Click
'Test if mask data entry completed
If mtbMaskTest.MaskCompleted Then
MessageBox.Show("Entry in mask is valid", "Valid Entry", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Entry in mask is INVALID", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Output:-