vb.net 日期时间选择器验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12961627/
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-13 15:58:59  来源:igfitidea点击:

DateTimePicker validation

vb.netdatetimepicker

提问by user1670625

I want to validate the DateTimePicker control in vb.net. I am using it for date of joining for the employee so i dont want the control to allow selection of future dates..only dates till the current date can be allowed to select. I have tried the following code:

我想验证 vb.net 中的 DateTimePicker 控件。我将它用于员工的加入日期,所以我不希望控件允许选择未来日期..只能允许选择当前日期之前的日期。我尝试了以下代码:

Private Sub DateTimePickerDOJ_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePickerDOJ.ValueChanged

    If DateTimePickerDOJ.Value > Date.Today Then
        MessageBox.Show("You Cannot Select a Future Date!", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
End Sub

The above code only displays the message correctly but its of no use becoz the future date is getting selected.

上面的代码只正确显示消息,但它没有用,因为选择了未来的日期。

can anyone please help me out with dis problem?

任何人都可以帮我解决dis问题吗?

采纳答案by fableal

You can use the DateTimePicker.MaxDateproperty to achieve that.

您可以使用DateTimePicker.MaxDate属性来实现这一点。

"Gets or sets the maximum date and time that can be selected in the control."

“获取或设置可以在控件中选择的最大日期和时间。”