将可为 null 的 DateTime 绑定到 MaskedTextBox

发布于 2024-07-23 06:41:06 字数 152 浏览 7 评论 0原文

我有一个绑定到 nullabe 日期时间的屏蔽文本框,但是当日期被清空时,屏蔽文本框上的验证将无法完成。 有没有办法强制这种行为? 我想要一个空白文本框等于空日期时间。

当文本框已经为空时,验证起作用。 只有当已经绑定了日期并且我尝试将其删除时,它才会中断。

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 behaviour? I want a blanked out text box to equal a 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏了南城 2024-07-30 06:41:06

我发现这与验证无关。 这是日期被解析回日期时间的时候。

这可能不是最优雅的方法,但它确实有效。 如果有人知道更好的方法,请告诉我。

我现在有这个代码。

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 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;
}
虚拟世界 2024-07-30 06:41:06

如果需要 null 日期值,我将其与 maskedtextbox 一起用于 datetime 类型

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

,请在类声明中使用可为空的日期时间类型:

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

I use this with maskedtextbox for datetime type

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 :

private DateTime? _DateBrth;
        public DateTime? DateBrth
        {
            get { return _DateBrth; }
            set { _DateBrth = value; }
        }
2024-07-30 06:41:06

这应该有效:

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;

}

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;

}
掩耳倾听 2024-07-30 06:41:06

经过试验,我终于找到了一个更简单的解决方案。

第 1 步:

在 Form.Designer.cs 中搜索绑定 maskedtextbox 的行(我的称为“mTFecha”)。 即:

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

第 2 步:

应用一个小技巧:

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

你就完成了!

Experimenting with this i finally found an easier solution to this.

STEP 1:

Search the line that is binding your maskedtextbox (mine's called "mTFecha") in your Form.Designer.cs. i.e:

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

STEP 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!

踏月而来 2024-07-30 06:41:06

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

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

You can simply give date format as below:

maskTextBox1.DataBindings.Add("Text", bs, "SummitDate1", true, DataSourceUpdateMode.OnPropertyChanged, null, "dd/MM/yyyy");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文