A MaskedTextBox control provides validation mechanism for user input on a Form. By default Masked property is set to None and the control works like a normal TextBox control.
How use MaskedTextBox Control
Drag and drop MaskedTextBox and Button (change its caption to Submit) control from toolbox on the window Form.
Code:
PublicClass Form10
PrivateSub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
'Here TextBox is masked as number show you can enter number only
MaskedTextBox1.Mask = "00000000000000000"
EndSub
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Enter number will show in Label
Label2.Text = "Entered Number is " + MaskedTextBox1.Text
EndSub
EndClass
Run The Project
When you enter other than number then masketextbox will not accept.Because in code mask is specified as MaskedTextBox1.Mask = "00000000000000000" which means it will accept only number.
When you enter number and click Submit Button then click event of submit button will fire and entered number will show in the Label.
MaskedTextBox Property
UseSystemPasswordChar: Gets or sets a value indicating as password character.
PrivateSub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
' entered number will show as password
MaskedTextBox1.UseSystemPasswordChar = True
EndSub
ForeColor:
ForeColor ofMaskedTextBox can be changed through ForeColor property of MaskedTextBox.
PrivateSub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
'change ForeColor of MaskedTextBox
MaskedTextBox1.ForeColor = Color.Red
EndSub
ForeColor of MaskedTextBox will bechanged.
Anonymous User
20-Apr-2019Thank You for the informative post.
Sunil Singh
06-Jul-2017This article is truly nice.