简体   繁体   English

将可空的DateTime绑定到MaskedTextBox

[英]Bind nullable DateTime to MaskedTextBox

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. 我有一个绑定到nullabe日期时间的蒙版文本框,但是当日期消隐时,屏蔽文本框上的验证将无法完成。 Is there a way to force this behaviour? 有没有办法强迫这种行为? I want a blanked out text box to equal a null DateTime. 我想要一个消隐的文本框等于null DateTime。

When the textbox is already null, the validation works. 当文本框已为空时,验证将起作用。 It only breaks when there is a date already bound and I try to blank it out. 只有在已经绑定的日期时它才会中断,我会尝试将其清空。

I figured out it didn't have to do with the validation. 我发现它与验证没有关系。 It was when the date was being parsed back to the datetime. 当日期被解析回日期时间时。

This may not be the most elegant way to do this, but it does work. 这可能不是最优雅的方式,但它确实有效。 If anyone knows a better way, please let me know. 如果有人知道更好的方法,请告诉我。

I have this code now. 我现在有这个代码。

public static void FormatDate(MaskedTextBox c) {
    c.DataBindings[0].Format += new ConvertEventHandler(Date_Format);
    c.DataBindings[0].Parse += new ConvertEventHandler(Date_Parse);
}

private static void Date_Format(object sender, ConvertEventArgs e) {
    if (e.Value == null)
        e.Value = "";
    else
        e.Value = ((DateTime)e.Value).ToString("MM/dd/yyyy");
}

static void Date_Parse(object sender, ConvertEventArgs e) {
    if (e.Value.ToString() == "  /  /")
        e.Value = null;
}

I use this with maskedtextbox for datetime type 我将这与maskedtextbox用于datetime类型

this.txtDateBrth.DataBindings.Add("Text", bsAgent, "DateBrth", true, DataSourceUpdateMode.OnPropertyChanged, null, "dd/MM/yyyy");

if need null date value, use nullable datetime type in class declaration : 如果需要null日期值,请在类声明中使用可空的datetime类型:

private DateTime? _DateBrth;
        public DateTime? DateBrth
        {
            get { return _DateBrth; }
            set { _DateBrth = value; }
        }

Experimenting with this i finally found an easier solution to this. 通过试验,我终于找到了一个更简单的解决方案。

STEP 1: 步骤1:

Search the line that is binding your maskedtextbox (mine's called "mTFecha") in your Form.Designer.cs. 在Form.Designer.cs中搜索绑定了maskedtextbox(我的名为“mTFecha”)的行。 ie: 即:

 // mTFecha
 // 
 this.mTFecha.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.listaAnimalesOfertadosBindingSource, "F_peso", true);

STEP 2: 第2步:

Apply a minor hack: 申请一个小黑客:

this.mTFecha.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.listaAnimalesOfertadosBindingSource, "F_peso", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, "  /  /"));

You're Done! 你完成了!

您可以简单地给出如下日期格式:

maskTextBox1.DataBindings.Add("Text", bs, "SummitDate1", true, DataSourceUpdateMode.OnPropertyChanged, null, "dd/MM/yyyy");

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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM