MaskedTextBox.ValidatingType 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定用於驗證使用者輸入資料的資料型態。
public:
property Type ^ ValidatingType { Type ^ get(); void set(Type ^ value); };
[System.ComponentModel.Browsable(false)]
public Type ValidatingType { get; set; }
[System.ComponentModel.Browsable(false)]
public Type? ValidatingType { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.ValidatingType : Type with get, set
Public Property ValidatingType As Type
屬性值
A Type 代表驗證中使用的資料型態。 預設值為 null。
- 屬性
範例
以下程式碼範例嘗試解析使用者的輸入為有效的 DateTime。 若失敗, TypeValidationCompleted 事件處理器會向使用者顯示錯誤訊息。 若值有效 DateTime,程式碼會進行額外檢查,以確保所提供的日期不早於今日日期。 這個程式碼範例要求你的 Windows Forms 專案包含 MaskedTextBox 一個名為 MaskedTextBox1 的控制項和 ToolTip 一個名為 ToolTip1的控制項。
private void Form1_Load(object sender, EventArgs e)
{
maskedTextBox1.Mask = "00/00/0000";
maskedTextBox1.ValidatingType = typeof(System.DateTime);
maskedTextBox1.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted);
maskedTextBox1.KeyDown += new KeyEventHandler(maskedTextBox1_KeyDown);
toolTip1.IsBalloon = true;
}
void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
{
if (!e.IsValidInput)
{
toolTip1.ToolTipTitle = "Invalid Date";
toolTip1.Show("The data you supplied must be a valid date in the format mm/dd/yyyy.", maskedTextBox1, 0, -20, 5000);
}
else
{
//Now that the type has passed basic type validation, enforce more specific type rules.
DateTime userDate = (DateTime)e.ReturnValue;
if (userDate < DateTime.Now)
{
toolTip1.ToolTipTitle = "Invalid Date";
toolTip1.Show("The date in this field must be greater than today's date.", maskedTextBox1, 0, -20, 5000);
e.Cancel = true;
}
}
}
// Hide the tooltip if the user starts typing again before the five-second display limit on the tooltip expires.
void maskedTextBox1_KeyDown(object sender, KeyEventArgs e)
{
toolTip1.Hide(maskedTextBox1);
}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MaskedTextBox1.Mask = "00/00/0000"
Me.MaskedTextBox1.ValidatingType = GetType(System.DateTime)
Me.ToolTip1.IsBalloon = True
End Sub
Private Sub MaskedTextBox1_TypeValidationCompleted(ByVal sender As Object, ByVal e As TypeValidationEventArgs) Handles MaskedTextBox1.TypeValidationCompleted
If (Not e.IsValidInput) Then
Me.ToolTip1.ToolTipTitle = "Invalid Date"
Me.ToolTip1.Show("The data you supplied must be a valid date in the format mm/dd/yyyy.", Me.MaskedTextBox1, 0, -20, 5000)
Else
' Now that the type has passed basic type validation, enforce more specific type rules.
Dim UserDate As DateTime = CDate(e.ReturnValue)
If (UserDate < DateTime.Now) Then
Me.ToolTip1.ToolTipTitle = "Invalid Date"
Me.ToolTip1.Show("The date in this field must be greater than today's date.", Me.MaskedTextBox1, 0, -20, 5000)
e.Cancel = True
End If
End If
End Sub
' Hide the tooltip if the user starts typing again before the five-second display limit on the tooltip expires.
Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MaskedTextBox1.KeyDown
Me.ToolTip1.Hide(Me.MaskedTextBox1)
End Sub
備註
遮罩本身並不保證使用者的輸入會代表特定類型的有效值。 以下 C# 程式碼顯示一個遮罩:
maskedTextBox1.Mask = "99/99/9999";
以下 Visual Basic 程式碼顯示一個遮罩:
MaskedTextBox1.Mask = "99/99/9999"
此掩罩可要求使用者輸入八位數字,但無法驗證使用者輸入的月份、日期及年份值是否正確;「12/20/20/2003」和「70/90/0000」對面具來說同樣有效。
你可以用來 ValidatingType 驗證使用者輸入的資料是否落在正確範圍內——在上述情況下,指派該類型實例 DateTime 。 控制項中目前的文字會在使用者離開控制項時被驗證。 你可以透過監控 TypeValidationCompleted 事件來判斷資料是否未通過驗證。
MaskedTextBox只有當 ValidatingType 是 時MaskCompleted才會執行檢定。true
如果你想使用自訂資料型別, ValidatingType必須實作一個靜態 Parse 方法,將字串作為參數。 此方法必須以以下其中一個或兩個簽章實作:
public static Object Parse(string)
public static Object Parse(string, IFormatProvider)