Click here to Skip to main content
14,930,588 members
Articles / Programming Languages / Visual Basic 10
Posted 11 Oct 2013


5 bookmarked

Using Masked TextBox in .NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
11 Oct 2013CPOL1 min read
How to use Masked Textbox control in .NET


Masked TextBox Control in .NET is used to restrict the input from user . Masked Edit controls are also used to change the format of output that is to be delivered. In VB 6, we have Mask Edit control, but in VB.NET, we can get Maskedtextbox from Toolbox.

Masked Text box control is similar to a simple Text box control. But it provides access to mask or change the format of input as well as output.

If you have defined Mask for validating input or output, then each character position in Masked text box checks the validation you provided. The Mask property does not allow you to enter Invalid Characters or input data into the control.

If you try to enter data that is invalid according to Mask Applied, then the control generates Validation Error. Now it will behave different from normal standard Text box control.

How to Add MaskedTextBox in .NET


  1. Click on Toolbox in the left-side Bar
  2. Navigate and Find MaskedTextBox .NET Component
  3. Double click on it to get it on Windows Form

How to Set Mask Property of Masked TextBox in .NET


  1. Click on Properties in Right-sidebar. It appears as shown in the image below
  2. Click on Set Mask or directly click on Browse button
  3. A dialog box appears put value according to your requirement in Mask labeled Textbox
  4. Click Ok

Design of App for using Masked TextBox in .NET

Code For Making this Demo App for using Masked TextBox in .NET
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

    MaskedTextBox1.Mask = "0000000"

    MaskedTextBox2.Mask = "0000000"

End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
    MaskedTextBox1.Mask = "0000000"
    MaskedTextBox2.Mask = "0000000"
    Dim a As Integer = Val(MaskedTextBox1.Text)
    Dim b As Integer = Val(MaskedTextBox2.Text)
    Dim c As Integer = Val(MaskedTextBox1.Text) - Val(MaskedTextBox2.Text)
Dim str As String = CInt(c)
If Len(str) = 1 Then
      MaskedTextBox3.Mask = "0"
    ElseIf Len(str) = 2 Then
      MaskedTextBox3.Mask = "00"
    ElseIf Len(str) = 3 Then
      MaskedTextBox3.Mask = "0,00"
    ElseIf Len(str) = 4 Then
      MaskedTextBox3.Mask = "0,000"
    ElseIf Len(str) = 5 Then
      MaskedTextBox3.Mask = "00,000"
    ElseIf Len(str) = 6 Then
      MaskedTextBox3.Mask = "0,00,000"
    ElseIf Len(str) = 7 Then
      MaskedTextBox3.Mask = "00,00,000"
End If
    MaskedTextBox3.Text = Val(MaskedTextBox4.Text) - Val(MaskedTextBox5.Text)
End Sub

Result of App for using Masked TextBox in .NET

  1. First, I have given 20 lakh as input - 2000000
  2. Then I have given input as 18 thousand - 18000
  3. Now subtraction is produced with commas 19,82,000


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Comments and Discussions

SuggestionNice! Pin
Member 122480856-Jan-16 7:04
MemberMember 122480856-Jan-16 7:04 
QuestionShould fix code Pin
IronRazer19-Sep-15 12:20
MemberIronRazer19-Sep-15 12:20 
Questionfix messed code formatting Pin
heemanshubhalla11-Oct-13 8:32
Memberheemanshubhalla11-Oct-13 8:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.