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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21561229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:39:39  来源:igfitidea点击:

Verifying a date is valid in a masked textbox in vb.net. Also, allow masked textbox to be empty

vb.netvalidationdatemaskedtextbox

提问by user3272229

I'm working in VB.Net. I have a Windows form in which I need to have users enter valid dates inbetween 01/01/1930 and 01/01/2099. I'm using masked textbox fields for my date fields. I want my date fields to validate the dates entered to make sure they are valid, but I also want the user to be able to tab through the field without having to enter any date at all. The code below appears to validate the dates. I can enter 2/29/2010, but the date will not be accepted because it is not a leap year. The problem is the field will not allow the user to leave it blank. Can someone help me to rewrite this code so that it works the way I intended?

我在 VB.Net 工作。我有一个 Windows 表单,我需要让用户在其中输入 01/01/1930 和 01/01/2099 之间的有效日期。我正在为我的日期字段使用掩码文本框字段。我希望我的日期字段验证输入的日期以确保它们有效,但我也希望用户能够在该字段中使用 Tab 键,而无需输入任何日期。下面的代码似乎用于验证日期。我可以输入2/29/2010,但是日期不会被接受,因为它不是闰年。问题是该字段不允许用户将其留空。有人可以帮我重写这段代码,使其按我的预期工作吗?

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

'Declaration Public Property ValidatingType As Type

'声明公共属性ValidatingType As Type

'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

Here is the code that ended up working for me:

这是最终对我来说有效的代码:

_________________________________________________________________________________________________
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


 'Declaration
Public Property ValidatingType As Type


Private Sub mskDOL_TypeValidationCompleted(ByVal sender As Object, ByVal e As TypeValidationEventArgs) Handles mskDOL.TypeValidationCompleted

Private Sub mskDOL_TypeValidationCompleted(ByVal sender As Object, ByVal e As 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

回答by Neolisk

You can achieve a similar behavior with a DateTimePicker:

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

enter image description here

在此处输入图片说明

Just set these:

只需设置这些:

  • Format = Custom
  • CustomFormat = MM/dd/yyyy
  • 格式 = 自定义
  • 自定义格式 = MM/dd/yyyy

Unless you need to enter dates before 1/1/1753, I see no reason why it would not work for you. Even if it does not work exactlylike you want it, extending DateTimePicker may end up being less effort. For example:

除非您需要在 之前输入日期1/1/1753,否则我认为它对您不起作用。即使不工作正是像你想的那样,延长的DateTimePicker最终可能事半功倍。例如:

With this approach, you can type a date by parts, i.e. month, day, year. To type a date as a whole in one shot - instead of creating your own custom control, you can try this workaround:

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