验证vb.net中屏蔽文本框中的日期是否有效。另外,允许屏蔽文本框为空

验证vb.net中屏蔽文本框中的日期是否有效。另外,允许屏蔽文本框为空,vb.net,validation,date,maskedtextbox,Vb.net,Validation,Date,Maskedtextbox,我在VB.Net中工作。我有一个Windows窗体,需要用户在其中输入1930年1月1日至2099年1月1日之间的有效日期。我正在为我的日期字段使用屏蔽文本框字段。我希望我的日期字段验证输入的日期,以确保它们有效,但我也希望用户能够在不必输入任何日期的情况下通过该字段进行制表。下面的代码用于验证日期。我可以输入2010年2月29日,但该日期不会被接受,因为它不是闰年。问题是该字段不允许用户将其留空。有人能帮我重写这段代码,让它按照我的预期工作吗 Private Sub frmSalvageMan

我在VB.Net中工作。我有一个Windows窗体,需要用户在其中输入1930年1月1日至2099年1月1日之间的有效日期。我正在为我的日期字段使用屏蔽文本框字段。我希望我的日期字段验证输入的日期,以确保它们有效,但我也希望用户能够在不必输入任何日期的情况下通过该字段进行制表。下面的代码用于验证日期。我可以输入2010年2月29日,但该日期不会被接受,因为它不是闰年。问题是该字段不允许用户将其留空。有人能帮我重写这段代码,让它按照我的预期工作吗

Private Sub frmSalvageManagerEntry_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Validate Date Fields
    Me.mskDOL.Mask = "00/00/0000"
    Me.mskDOL.ValidatingType = GetType(System.DateTime)
    Me.mskSetupDate.Mask = "00/00/0000"
    Me.mskSetupDate.ValidatingType = GetType(System.DateTime)

End Sub
"宣言", 公共属性ValidatingType作为类型

'Validate the date the user enters into the mskDOL field
Private Sub mskDOL_TypeValidationCompleted(ByVal sender As Object, ByVal e As TypeValidationEventArgs) Handles mskDOL.TypeValidationCompleted

    If mskDOL.Text = "" Then
        e.Cancel = True
        Exit Sub
    End If


    If (Not e.IsValidInput) Then
        MessageBox.Show("Please input a valid date in the format mm/dd/yyyy.", "Invalid Date Entered", MessageBoxButtons.RetryCancel)
        mskDOL.Focus()
        SendKeys.Send("{End}")

    Else
        Dim UserDate As DateTime = CDate(e.ReturnValue)
        If (UserDate < "01/01/1930" Or UserDate > "01/01/2099") Then
            MessageBox.Show("Please input a valid date in the format mm/dd/yyyy.", "Invalid Date Entered", MessageBoxButtons.RetryCancel)
            e.Cancel = True
            mskDOL.Focus()
            SendKeys.Send("{End}")
        End If
    End If


End Sub


私有子mskDOL_TypeValidationCompleted(ByVal发送方作为对象,ByVal e作为TypeValidationEventArgs)处理mskDOL.TypeValidationCompleted

    'Make sure the properties of the masked textbox field in the user interface are set to the following:
    'TextMaskFormat = IncludePromptAndLiterals
    'CutCopyMaskFormat = IncludeLiterals
    'Don't add a mask to the Mask property of the masked textbox because it is written in the form load event.
    'All the other properties can be left as default


    'If the user does not have a date to enter, allow them to tab through the field.
    If mskDOL.Text = "__/__/____" Then
        Exit Sub

    End If

    'Test to see if a valid date has been input.  If not, the user will get a message asking them to reenter a valid date.
    If (Not e.IsValidInput) Then
        MessageBox.Show("Please input a valid date in the format mm/dd/yyyy.", "Invalid Date Entered", MessageBoxButtons.RetryCancel)
        mskDOL.Focus()
        SendKeys.Send("{End}")

        'If a valid date has been entered, test to see if the user has entered a date between 01/01/1930 and 01/01/2099.
        'If the user has not entered a date within the proper timeframe, they will get a message asking them to enter a date between 01/01/1930 and 01/01/2099.
    Else
        Dim DOLDate As DateTime = CDate(e.ReturnValue)
        If (DOLDate < "01/01/1930" Or DOLDate > "01/01/2099") Then
            MessageBox.Show("Please input a valid date between 01/01/1930 to 01/01/2099 in the format mm/dd/yyyy.", "Invalid Date Entered", MessageBoxButtons.RetryCancel)
            mskDOL.Focus()
            SendKeys.Send("{End}")
            e.Cancel = True

        End If
    End If

End Sub
'确保用户界面中屏蔽文本框字段的属性设置为以下值:
'TextMaskFormat=IncludePromptAndLiterals
'CutCopyMaskFormat=IncludeTerrals
'不要向掩码文本框的掩码属性添加掩码,因为它是在form load事件中写入的。
'所有其他属性都可以保留为默认值
'如果用户没有日期可输入,则允许他们在该字段中进行制表。
如果mskDOL.Text=“\uuuuu/\uuuuu/\uuuuuuuu/”则
出口接头
如果结束
'测试以查看是否输入了有效日期。如果没有,用户将收到一条消息,要求他们重新输入有效日期。
如果(不是e.IsValidInput),则
MessageBox.Show(“请以mm/dd/yyyy的格式输入有效日期。”,“输入的日期无效”,MessageBox按钮。RetryCancel)
mskDOL.Focus()
SendKeys.Send(“{End}”)
'如果输入了有效日期,测试用户是否输入了1930年1月1日至2099年1月1日之间的日期。
'如果用户没有在适当的时间范围内输入日期,他们将收到一条消息,要求他们输入1930年1月1日至2099年1月1日之间的日期。
其他的
Dim DOLDate As DateTime=CDate(例如返回值)
如果(DOLDate<“01/01/1930”或DOLDate>“01/01/2099”),则
MessageBox.Show(“请以mm/dd/yyyy格式输入1930年1月1日至2099年1月1日之间的有效日期。”,“输入的日期无效”,MessageBox按钮。RetryCancel)
mskDOL.Focus()
SendKeys.Send(“{End}”)
e、 取消=真
如果结束
如果结束
端接头

您可以使用DateTimePicker实现类似的行为:

只需设置这些:

  • 格式=自定义
  • CustomFormat=MM/dd/yyyy
除非您需要在
1/1/1753
之前输入日期,否则我看不出它不适合您的原因。即使它不能完全按照您想要的方式工作,扩展DateTimePicker也可能会减少工作量。例如:

使用这种方法,您可以按部分键入日期,即月、日、年。要一次性键入整个日期,您可以尝试以下解决方法,而不是创建自己的自定义控件:


如果日期为空,是否要退出子功能?如果是这样,您可以尝试这样做:
如果mskDOL.Text=“\uuuuuuu/\uuuuuu/\uuuuuuuuuuu/”然后在
mskDOL\u TypeValidationCompleted
的开头退出Sub
,您还可以将日期时间选择器配置为类似于文本框。只需将默认日期设置为从未使用过的值。现在,您的值将自动验证。这增加了从日历中选择日期的选项。这很有效!谢谢我试过DateTimePicker,但我不知道如何让控件让我输入日期,而不是通过单击来选择日期。我觉得不能输入日期很烦人,所以我把这个控件作为一个选项,我把实际有效的代码添加到了原始问题的末尾。谢谢你的帮助!
 'Declaration
Public Property ValidatingType As Type
    'Make sure the properties of the masked textbox field in the user interface are set to the following:
    'TextMaskFormat = IncludePromptAndLiterals
    'CutCopyMaskFormat = IncludeLiterals
    'Don't add a mask to the Mask property of the masked textbox because it is written in the form load event.
    'All the other properties can be left as default


    'If the user does not have a date to enter, allow them to tab through the field.
    If mskDOL.Text = "__/__/____" Then
        Exit Sub

    End If

    'Test to see if a valid date has been input.  If not, the user will get a message asking them to reenter a valid date.
    If (Not e.IsValidInput) Then
        MessageBox.Show("Please input a valid date in the format mm/dd/yyyy.", "Invalid Date Entered", MessageBoxButtons.RetryCancel)
        mskDOL.Focus()
        SendKeys.Send("{End}")

        'If a valid date has been entered, test to see if the user has entered a date between 01/01/1930 and 01/01/2099.
        'If the user has not entered a date within the proper timeframe, they will get a message asking them to enter a date between 01/01/1930 and 01/01/2099.
    Else
        Dim DOLDate As DateTime = CDate(e.ReturnValue)
        If (DOLDate < "01/01/1930" Or DOLDate > "01/01/2099") Then
            MessageBox.Show("Please input a valid date between 01/01/1930 to 01/01/2099 in the format mm/dd/yyyy.", "Invalid Date Entered", MessageBoxButtons.RetryCancel)
            mskDOL.Focus()
            SendKeys.Send("{End}")
            e.Cancel = True

        End If
    End If

End Sub