65.9K
CodeProject is changing. Read more.
Home

Using Masked TextBox in .NET

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Oct 11, 2013

CPOL

1 min read

viewsIcon

147399

downloadIcon

4

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