hi there,

i have asked this question several times but i coudn't solve this problem.

i have two datetime picker one as the start date and the othere as the end date. when the user clicks a date in the start date he should not select a date that has passed. but when I try to select a passed date in a previous date the error msg appear several times. why is this happening.

i have coded in the closeup and the value change event
how can i solve this.

thanxx

private void dpSdate_CloseUp(object sender, EventArgs e)
        {
            if (dpSdate.Value < System.DateTime.Now.Date)
            {
                dpSdate.Validating -= new CancelEventHandler(dpSdate_ValueChanged);
                dpSdate.Text = System.DateTime.Now.Date.ToString();
                dpSdate.Validating += new CancelEventHandler(dpSdate_ValueChanged);
            }
        }

        private void dpEdate_ValueChanged(object sender, EventArgs e)
        {
            if (dpEdate.Value < dpSdate.Value)
            {
                dpEndDateC++; 
                if (dpEndDateC == 2)
                    MessageBox.Show("Enter date greater than start date");
            }
        }

Have you tried using the MinDate and MaxDate properties of the DateTimePicker.
I know from previous posts that you want to restrict the start date to be from today onwards so to do this you can just set the MinDate property of dpSdate.
To get one date to be restricted to the others value you can bind the datetime picker MaxDate/MinDate to the others value. E.g.
By adding the following code in your form_load you will not need to do the testing in ValueChanged events.

// set start date to initial value of today
dpSdate.Value = DateTime.Today;
// set end date to initial value of today + 1 day
dpEdate.Value = DateTime.Today.AddDays(1);
// restrict start date to start from today onwards
dpSdate.MinDate = DateTime.Today;
// restrict start date to a max value of end date
dpSdate.DataBindings.Add(new Binding("MaxDate", dpEdate, "Value"));
// restrict end date to a min value of start date
dpEdate.DataBindings.Add(new Binding("MinDate", dpSdate, "Value"));

Doing this prevents the user from entering invalid values in the first place but could give the feeling that your code is not working. So you will need to inform the user that this restriction is in place but this could be done by a label or tooltip.

If you still want to use ValueChanged and display messages (a valid solution), then please post your dpSdate_ValueChanged code and explain how dpEndDateC is used.

Hope this helps and please remember to mark solved if your issue is resolved.

Edited 6 Years Ago by nick.crane: n/a

Have you tried using the MinDate and MaxDate properties of the DateTimePicker.
I know from previous posts that you want to restrict the start date to be from today onwards so to do this you can just set the MinDate property of dpSdate.
To get one date to be restricted to the others value you can bind the datetime picker MaxDate/MinDate to the others value. E.g.
By adding the following code in your form_load you will not need to do the testing in ValueChanged events.

// set start date to initial value of today
dpSdate.Value = DateTime.Today;
// set end date to initial value of today + 1 day
dpEdate.Value = DateTime.Today.AddDays(1);
// restrict start date to start from today onwards
dpSdate.MinDate = DateTime.Today;
// restrict start date to a max value of end date
dpSdate.DataBindings.Add(new Binding("MaxDate", dpEdate, "Value"));
// restrict end date to a min value of start date
dpEdate.DataBindings.Add(new Binding("MinDate", dpSdate, "Value"));

Doing this prevents the user from entering invalid values in the first place but could give the feeling that your code is not working. So you will need to inform the user that this restriction is in place but this could be done by a label or tooltip.

If you still want to use ValueChanged and display messages (a valid solution), then please post your dpSdate_ValueChanged code and explain how dpEndDateC is used.

Hope this helps and please remember to mark solved if your issue is resolved.

hey thanxx

This question has already been answered. Start a new discussion instead.