Raise validating event on datepicker c# Winforms

621 Views Asked by At

I have 2 datepickers. One with invoicedate and one with due date. It is possible for me to pick a date for the invoice on tomorrow. But then when I continue filling in the controls, its possible to just leave the due date on today. This gives the scenario where the duedate event is not being fired, because I did not enter it. Now, I don't want the user to have a due date that is smaller than the actual invoice date, but as the event of "due date" is not being fired, I can't really validate this.

Could anyone tell me how to fire the validating event by code?

This is the scenario that I have for the moment:

   private void dpInvoiceDate_Validating(object sender, CancelEventArgs e)
    {
        // Convert the dp invoice date + hour to only date 
        var dateAndTime = Convert.ToDateTime(dpInvoiceDate.Text);
        var date = dateAndTime.Date;



        if (!InputChecks.IsGeldigeDatum(DateTime.Now.Date, Convert.ToDateTime(date)))
        {
            errorProvider1.SetError(dpInvoiceDate, "Invoice date can not be in the past");
            e.Cancel = true;
        }
        else
        {
            errorProvider1.SetError(dpInvoiceDate, "");
        }


    }

    private void dpDueDate_Validating(object sender, CancelEventArgs e)
    {
        // Convert the dp invoice date + hour to only date 
        var dateAndTime = Convert.ToDateTime(dpDueDate.Text);
        var date = dateAndTime.Date;
        var dateAndTimeInvioceDate = Convert.ToDateTime(dpInvoiceDate.Text);
        var dateInvoiceDate = dateAndTimeInvioceDate.Date;



        if (date < dateInvoiceDate)
        {
            errorProvider1.SetError(dpDueDate, "Due date can not be in the past");
            e.Cancel = true;
        }
        else
        {
            errorProvider1.SetError(dpDueDate, "");
        }
    }
1

There are 1 best solutions below

1
progpow On

My recomends to call method Update() at validating invoice date.

dpDueDate.Update();