Friday, 11 October 2013

Masked TextBox in .NET

Masked TextBox Control in .NET are 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 check 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

http://geeksprogrammings.blogspot.com/2013/10/maskedtextboxin.NET.html
geeksprogrammings.blogspot.in


How To Set Mask Property of Masked TextBox  in .NET

STEPS :-
1. Click on Properties in Right-sidebar . It appears as shown in 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 labelled Textbox
4. Click Ok
http://geeksprogrammings.blogspot.com/2013/10/maskedtextboxin.NET.html
geeksprogrammings.blogspot.in

Design of App for using Masked TextBox in .NET
http://geeksprogrammings.blogspot.com/2013/10/maskedtextboxin.NET.html
geeksprogrammings.blogspot.in
Design Consist of Two TextBox and One Masked TextBox for Result and A Button for Getting Result



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"   // setting initial Mask
        MaskedTextBox2.Mask = "0000000"    // setting initial Mask

    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

http://geeksprogrammings.blogspot.com/2013/10/maskedtextboxin.NET.html
geeksprogrammings.blogspot.in

Share this

2 Responses to "Masked TextBox in .NET"

  1. wooooooo. ....... after 2 days of search atlast i have found how to use masked textbox and how to use commans ...........big thanks to author

    ReplyDelete
  2. i am having some problem with my code but thanks to you for this guidance

    ReplyDelete