MaskedTextBox.Text Property

Definition

Gets or sets the text as it is currently displayed to the user.

C#
[System.ComponentModel.Bindable(true)]
public override string Text { get; set; }

Property Value

A String containing the text currently displayed by the control. The default is an empty string.

Attributes

Examples

The following code example demonstrates the use of the Text property.

VB
Private Sub DisplayText()
    Me.MaskedTextBox1.PasswordChar = CChar("*")

    Me.MaskedTextBox1.Mask = "000-00-0000" ' United States Social Security Number
    Me.MaskedTextBox1.Text = "999999999"

    Debug.WriteLine("MaskedControl.Text: " & Me.MaskedTextBox1.Text) ' Displays: 999-99-9
    Me.MaskedTextBox1.Text = ""

    ' Assigning text.
    Me.MaskedTextBox1.AllowPromptAsInput = True
    Me.MaskedTextBox1.Text = "999-99-9999" ' Works
    Me.MaskedTextBox1.Text = "999999999" ' Works
    Me.MaskedTextBox1.AllowPromptAsInput = False
    'Me.MaskedTextBox1.Text = "999-99-9999" ' Does not work
End Sub

Remarks

Text is the default binding property for the MaskedTextBox class.

Strings retrieved using this property are formatted according to the control's formatting properties, such as Mask and TextMaskFormat.

You can assign a string with or without literal characters to Text depending on the values of ResetOnPrompt, ResetOnSpace, and SkipLiterals.

Text is the default binding property for MaskedTextBox.

MaskedTextBox will raise the TextChanged event whenever the formatted text value changes. Different properties may or may not cause this value to change. For example, changing the PasswordChar property will not raise the TextChanged event, but changing the Mask property will.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also