MaskedTextBox Control in VB.NET
MaskedTextBoxes can be thought as the extension of MaskedEdit Control of VB6.0. It can be used to validate TextBox input based on ‘Mask’
The MaskedTextBox class is an enhanced TextBox control that supports a declarative syntax for accepting or rejecting user input. Using the Mask property, you can specify the following input without writing any custom validation logic in your application:
· Required input characters.
· Optional input characters.
· The type of input expected at a given position in the mask; for example, a digit, or an alphabetic or alphanumeric character.
· Mask literals, or characters that should appear directly in the MaskedTextBox; for example, the hyphens (-) in a phone number, or the currency symbol in a price.
· Special processing for input characters; for example, to convert alphabetic characters to uppercase.
When a MaskedTextBox control is displayed at run time, it represents the mask as a series of prompt characters and optional literal characters. Each editable mask position, representing a required or optional input, is shown with a single prompt character. For example, the number sign (#) is often used as a placeholder for a numeric character input. You can use the PromptChar property to specify a custom prompt character. The HidePromptOnLeave property determines if the user sees the prompt characters when the control loses input focus.
As the user types input into the masked text box, valid input characters replace their respective prompt characters in a sequential fashion. If the user types an invalid input character, no replacement occurs, but instead a beep is issued if the BeepOnError property is set to true, and the MaskInputRejected event is raised. You can provide your own custom error logic by handing this event.
Private Sub MaskedTextBox1_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected
If MaskedTextBox1.MaskFull Then
MsgBox("Please do not attempt to type extra data")
ElseIf e.Position = MaskedTextBox1.Mask.Length Then
MsgBox("End of Input: If necessary modify appropriate data by typeover")
Else
MsgBox("Only numerals are accepted")
End If
End Sub
When the current insertion point is at a literal character, the user has a number of options:
· If a character other than the prompt character is typed, the literal will automatically be skipped and the input character will be applied to the next editable position, represented by the next prompt character.
· If the prompt character is typed and the AllowPromptAsInput property is true, the input will overtype the prompt character and insertion point will be moved to the next position in the mask.
· As is always the case, the arrow keys can be used to navigate to a previous or subsequent position.
You can use the MaskFull property to verify whether or not the user has entered all of the required input. The Text property will always retrieve the user's input formatted according to the mask and the TextMaskFormat property.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sBoardingCity As String
Dim sDestnCity As String
sBoardingCity = DomainUpDownSource.Text
sDestnCity = DomainUpDownDestn.Text
If MaskedTextBox1.MaskFull = False Then
MsgBox("Please complete the form")
MaskedTextBox1.Focus()
End If
End Sub
The MaskedTextBox control actually defers all mask processing to the System.ComponentModel..::.MaskedTextProvider class specified by the MaskedTextProvider property. This standard provider supports all Unicode characters except for surrogates and vertically combined characters; however, the AsciiOnly property can be used to restrict input to the characters sets a-z, A-Z, and 0-9.
Masks do not necessarily guarantee that a user's input will represent a valid value for a given type; for example, -9 could be entered for an age in years. You can verify that a user's input represents a valid value by assigning an instance of that value's type to the ValidatingType property. You can detect whether the user removes focus from MaskedTextBox when it contains an invalid value by monitoring for the TypeValidationCompleted event. If type validation succeeds, the object representing the value will be available through the ReturnValue property of the TypeValidationEventArgs parameter.
As with the TextBox control, several common keyboard shortcuts do not work with MaskedTextBox. In particular, CTRL-R (right justify text), CTRL-L (left justify text), and CTRL-L (center text) have no effect.
See Also
Extract Ref Links From WebPage using VB.Net Regular Expressions
Remove HTML Tags from String using .NET Regular Expressions
VB.NET Regular Expression to Check URL
VB.NET Regular Expression to Check Email Addresses
VB.NET Regular Expression to Check MAC Address
Regular Expression to Check Zip Code
Validate eMail Addresses using VB.NET Function
Regular Expressions in Dot Net (.NET)
Hi very informative...
ReplyDeleteAlso check www.phonevalidator.com
I am not a advertiser of this company but i came across this site when i was searching for something else.
Thank You!
Hai Ritchie ,
ReplyDeleteI want to change the mask to be dd/MM/yyyy instead of __/__/____?
This was very helpful!
ReplyDeletehow to use masked textbox to display result of subs , addition as '10,000' with commas
ReplyDeletehttp://geeksprogrammings.blogspot.in/2013/10/maskedtextboxin.NET.html