简体   繁体   English

在 C# 中限制“datetimepicker”中的日期

[英]Restrict dates in 'datetimepicker' in C#

A feature of a forms based application I am developing allows the user to search through a history of records.我正在开发的基于表单的应用程序的一个功能允许用户搜索历史记录。

The user can search by name, by number, and between dates, and populate the results in a datagridview control.用户可以按名称、数字和日期之间进行搜索,并将结果填充到 datagridview 控件中。

这是表格:

However, as the form will be used to search for previous records.但是,由于表单将用于搜索以前的记录。 The ability for the user to select future dates is not required.用户选择未来日期的能力不是必需的。

Is there a way to prevent the user from selecting future dates, or even grey the future dates out?有没有办法阻止用户选择未来的日期,甚至将未来的日期变灰?

You can try with code based on MaxDate property您可以尝试使用基于MaxDate属性的代码

yourControl.MaxDate = DateTime.Today;

Link : http://msdn.microsoft.com/fr-fr/library/system.windows.forms.datetimepicker.maxdate.aspx链接: http : //msdn.microsoft.com/fr-fr/library/system.windows.forms.datetimepicker.maxdate.aspx

You can use the DateTimePicker.MaxDate Property:您可以使用DateTimePicker.MaxDate属性:

DateTimePicker.MaxDate Property : Gets or sets the maximum date and time that can be selected in the control. DateTimePicker.MaxDate 属性:获取或设置控件中可选择的最大日期和时间。

请尝试:

dateTimePicker.MaxDate = DateTime.Now;

Setting the MaxDate property will hide all dates after the specified date, and will not allow dates to be entered that are greater than that date.设置 MaxDate 属性将隐藏指定日期之后的所有日期,并且不允许输入大于该日期的日期。

See http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.maxdate.aspx for details.有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.maxdate.aspx

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

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