我在VB.Net工作。我有一个Windows窗体,我需要让用户在01/01/1930和01/01/2099之间输入有效日期。我正在为我的日期字段使用蒙面文本框字段。我希望我的日期字段验证输入的日期以确保它们有效,但我还希望用户能够在字段中进行选项卡而无需输入任何日期。以下代码似乎验证日期。我可以输入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 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
以下是最终为我工作的代码:
_________________________________________________________________________________________________
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)处理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
答案 0 :(得分:1)
您可以使用DateTimePicker实现类似的行为:
设置这些:
除非您需要在1/1/1753
之前输入日期,否则我认为没有理由不适合您。即使它不像你想要的那样完全,扩展DateTimePicker也可能会减少工作量。例如:
使用此方法,您可以按部分键入日期,即月,日,年。要一次性键入日期 - 而不是创建自己的自定义控件,您可以尝试此解决方法: