2012-02-09 18 views
2

WinFormsアプリケーションでマスクされたテキストボックスから日付変数を抽出する際に問題があります。マスクされたTextBoxの検証テキストエラー

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

をしかし、私はとNullReferenceExceptionエラーを取得し、マスクされたテキストボックスがdeinately NULLでない場合でも、次のようにユーザーの入力日付を読み取ろうと コードです。マスクされたテキストボックスに

プロパティが含まれます:

マスク:00/00/0000 検証タイプ:日時 TextMaskFormat:IncludeLiterals

これは私が以前のアプリにマスクされたテキストボックスを使用しているとまったく同じで、それで、今はどうしてですか?

誰でも私が間違っていることを確認できますか?ここで

+0

ValidateText() 'ValidatingType'によって指定されているように、あなたは1を持っています...それが変換? – V4Vendetta

答えて

1

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

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

関連する問題