Bind nullable DateTime to MaskedTextBox

后端 未结 5 1596
遇见更好的自我
遇见更好的自我 2021-01-18 05:59

I have a masked text box bound to a nullabe datetime, but when the date is blanked out, the validation on the masked text box won\'t complete. Is there a way to force this

5条回答
  •  旧巷少年郎
    2021-01-18 06:07

    This should work:

    private void Form1_Load(object sender, EventArgs e)
    {
        maskedTextBox1.Mask = "00/00/0000";
        maskedTextBox1.ValidatingType = typeof(System.DateTime);
        maskedTextBox1.TypeValidationCompleted += new TypeValidationEventHandler
           (maskedTextBox1_TypeValidationCompleted);
    }
    
    
    
    private void TypeValidationCompletedHandler(object sender, TypeValidationEventArgs e )
    {
        e.Cancel = !e.IsValidInput &&
            this.maskedTextBox1.MaskedTextProvider.AssignedEditPositionCount == 0;
    
    }
    

提交回复
热议问题