Click here to Skip to main content
15,886,788 members
Articles / Programming Languages / Visual Basic 10
Tip/Trick

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 145.6K   4   5   3
How to use Masked Textbox control in .NET

Introduction

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

Steps

  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

Steps

  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
VB
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

License

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



Comments and Discussions

 
SuggestionNice! Pin
Member 122480856-Jan-16 7:04
Member 122480856-Jan-16 7:04 
QuestionShould fix code Pin
IronRazer19-Sep-15 12:20
IronRazer19-Sep-15 12:20 
Questionfix messed code formatting Pin
heemanshubhalla11-Oct-13 8:32
heemanshubhalla11-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.