逐步解說:使用 MaskedTextBox 控制項

這個逐步解說中所述的工作包括:

建立專案並新增控制項

將 MaskedTextBox 控制項新增至表單

  1. 開啟您要放置控制項的 MaskedTextBox 表單。

  2. 將控制項從 [工具箱] MaskedTextBox 拖曳至您的表單。

  3. 以滑鼠右鍵按一下控制項,然後選擇 [ 屬性 ]。 在 [ 屬性] 視窗中,選取 [遮罩 ] 屬性,然後按一下 屬性名稱旁邊的 [...] (省略號) 按鈕。

  4. 在 [ 輸入遮罩] 對話方塊中,選取 [簡短日期 ] 遮罩,然後按一下 [ 確定 ]。

  5. 在 [ 屬性] 視窗中,將 BeepOnError 屬性設定為 true 。 每當使用者嘗試輸入違反遮罩定義的字元時,這個屬性就會發出簡短的嗶聲。

如需 Mask 屬性所支援的字元摘要,請參閱 屬性的 Mask 一節。

提醒使用者輸入錯誤

新增拒絕遮罩輸入的氣球提示

  1. 返回 [ 工具箱 ],並將 新增 ToolTip 至您的表單。

  2. MaskInputRejected 發生輸入錯誤時引發 ToolTip 的事件建立事件處理常式。 氣球提示會維持五秒的可見狀態,或直到使用者按一下它為止。

    public void Form1_Load(Object sender, EventArgs e)
    {  
        ... // Other initialization code  
        maskedTextBox1.Mask = "00/00/0000";  
        maskedTextBox1.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected)  
    }  
    
    void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)  
    {  
        toolTip1.ToolTipTitle = "Invalid Input";  
        toolTip1.Show("We're sorry, but only digits (0-9) are allowed in dates.", maskedTextBox1, maskedTextBox1.Location, 5000);  
    }  
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
        Me.ToolTip1.IsBalloon = True  
        Me.MaskedTextBox1.Mask = "00/00/0000"  
    End Sub  
    
    Private Sub MaskedTextBox1_MaskInputRejected(sender as Object, e as MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected  
        ToolTip1.ToolTipTitle = "Invalid Input"  
        ToolTip1.Show("We're sorry, but only digits (0-9) are allowed in dates.", MaskedTextBox1, 5000)  
    End Sub  
    

將使用者警示為不正確類型

新增無效資料類型的批註提示

  1. 在表單的事件處理常式中 Load ,將代表 DateTime 類型的物件指派 TypeMaskedTextBox 控制項的 ValidatingType 屬性:

    private void Form1_Load(Object sender, EventArgs e)  
    {  
        // Other code  
        maskedTextBox1.ValidatingType = typeof(System.DateTime);  
        maskedTextBox1.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted);  
    }  
    
    Private Sub Form1_Load(sender as Object, e as EventArgs)  
        // Other code  
        MaskedTextBox1.ValidatingType = GetType(System.DateTime)  
    End Sub  
    
  2. 新增 TypeValidationCompleted 事件的事件處理常式:

    public void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e)  
    {  
        if (!e.IsValidInput)  
        {  
           toolTip1.ToolTipTitle = "Invalid Date Value";  
           toolTip1.Show("We're sorry, but the value you entered is not a valid date. Please change the value.", maskedTextBox1, 5000);  
           e.Cancel = true;  
        }  
    }  
    
    Public Sub MaskedTextBox1_TypeValidationCompleted(sender as Object, e as TypeValidationEventArgs)  
        If Not e.IsValidInput Then  
           ToolTip1.ToolTipTitle = "Invalid Date Value"  
           ToolTip1.Show("We're sorry, but the value you entered is not a valid date. Please change the value.", maskedTextBox1, 5000)  
           e.Cancel = True  
        End If  
    End Sub  
    

另請參閱