蒙面文本框验证文本错误 [英] Masked TextBox Validating Text Error

查看:104
本文介绍了蒙面文本框验证文本错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在遇到一些麻烦从WinForms应用程序一个蒙面文本中提取日期变量。
中的代码,尝试读取用户输入的日期如下:

I'm having some trouble with date variables extracted from a masked textbox on a WinForms app. The code which attempts to read the user input date is as follows:

DateTime datExpDate = new DateTime();
datExpDate = (DateTime)txtExpDate.ValidateText();     



不过,我得到一个NullReferenceException错误,甚至当掩码文本框deinately不为null。

But I get a NullReferenceException error, even when the masked text box is deinately not Null.

在掩码文本框中的属性包括:

The properties on the masked text box include:

面膜:00/00/0000
验证类型:日期时间
TextMaskFormat:IncludeLiterals

Mask: 00/00/0000 Validating Type: DateTime TextMaskFormat: IncludeLiterals

这是完全一样的我用以前的应用程序蒙面文本框,然后它的工作,那么为什么不现在

This is exactly as I have used masked textboxes on previous apps and it worked then, so why not now?

任何人都可以发现什么,我干什么错了吗?

Can anyone spot what I am doin wrong please?

推荐答案

下面是从溶液MSDN:

Here is the solution from MSDN:

    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);
}



链接: http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.validatingtype的.aspx

这篇关于蒙面文本框验证文本错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆