3

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, "");
        }
    }
mhatch
  • 4,441
  • 6
  • 36
  • 62
Kevin
  • 151
  • 4
  • 15
  • I recommend using the Dispatcher.Invoke method at the point where you wish to invoke the duedate event https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.invoke%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 – Needham May 31 '16 at 22:40
  • Are you using Winforms validate mechanism? – progpow May 31 '16 at 23:10
  • @progpow: what exactly do you mean with validate mechanism? In my save button im calling '(this.ValidateChildren())' But even clicking the button doesn't return me the error. Or atleast it is not showing my errorprovider. – Kevin Jun 01 '16 at 08:01
  • @Needham: Im not able to access the dispatcher.invoke method. In the microsoft link you provide they start with "in WPF". Is this possible in winforms also, because VS is not asking me to add the reference. – Kevin Jun 01 '16 at 08:41

1 Answers1

0

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

dpDueDate.Update();
progpow
  • 1,560
  • 13
  • 26