阻止DateTimePicker2进入DateTimePicker1设置的日期

时间:2015-09-04 12:25:19

标签: c# datetimepicker

我有两个名为dtpFromDate的dateTimePicker和一个具有“日期范围”功能的dtpToDate。如何使dtpToDate的值不低于dtpFromDate的值?现在,我的dtpToDate可以低于dtpFromDate的值。

1 个答案:

答案 0 :(得分:0)

假设它的Winforms有几种方法可以做到,ASP.net会类似。

您需要在To Date选择器上添加Value Changed处理程序,或者根据您希望的方式(或两者)添加From来

    private void dtpFromDate_ValueChanged(object sender, EventArgs e)
    {
        //Set the min date of the to date when the from date is changed
        dtpToDate.MinDate = dtpFromDate.Value;
    }

    private void dtpToDate_ValueChanged(object sender, EventArgs e)
    {
        //set the to date to the date selected, or the 'from' date if the 'to' date is lower
        //(if this) ? (then this) : (otherwise this)
        dtpToDate.Value = dtpToDate.Value < dtpFromDate.Value ? dtpFromDate.Value : dtpToDate.Value;
    }