简体   繁体   English

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

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

I'm working in VB.Net. 我正在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. 我有一个Windows窗体,我需要用户在其中输入01/01/1930和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. 我可以输入2/29/2010,但由于它不是a年,所以不会接受该日期。 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? 有人可以帮我重写此代码,使其按我预期的方式工作吗?

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为类型

'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 私有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

You can achieve a similar behavior with a DateTimePicker: 您可以使用DateTimePicker实现类似的行为:

在此处输入图片说明

Just set these: 只需设置这些:

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

Unless you need to enter dates before 1/1/1753 , I see no reason why it would not work for you. 除非您需要输入1/1/1753年1月1日之前的日期, 1/1/1753我认为没有任何理由对您不起作用。 Even if it does not work exactly like you want it, extending DateTimePicker may end up being less effort. 即使它不能完全按照您的期望工作,但扩展DateTimePicker可能会减少工作量。 For example: 例如:

With this approach, you can type a date by parts, ie 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: 要一次完整地输入日期-您可以尝试以下解决方法,而不是创建自己的自定义控件:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM