hi,

i have a two datetime picker in a C# form,
when i select a earlier date (before today date) there is a caparison i have addded to the validateing event in the date timepicker.below is the code.

Question 1:
for some reason say that i add 10th may, the messagebox msg will iterate until it comes to the current date.why is this happening.

Question 2:
the evet is fired only whe i select the other datetime picker.them is will give the previous datetimepicker message box error and open the datetimepicker of the secomd datetimepicker combo box

how can i solve this,


please help
thanx

appriciate a lot
thanxxxx

if (System.DateTime.Compare(dpStartD.Value, DateTime.Now) < 0)
            {
                System.Windows.Forms.MessageBox.Show("Invalid Session Start Date");
                dpStartD.Text = DateTime.Now.ToString();
            }

krishnisilva,

The Validating event is fired when the user leaves a control.
It is possible to prevent the user from leaving the control if the value is invalid by setting Cancel on the CancelEventArgs parameter to true.

private void dpStartD_Validating(object sender, CancelEventArgs e)
{
   if (System.DateTime.Compare(dpStartD.Value, DateTime.Now) < 0)
   {
      System.Windows.Forms.MessageBox.Show("Invalid Session Start Date");
      dpStartD.Text = DateTime.Now.ToString();
      e.Cancel = true;
   }
}

Alternatively move you code to the ValueChanged event.

Nick.

there is no cancel in ValueChanged for the datetime picker.
how can i solve this???????

thanxxx

The ValueChanged event is triggered immediately after the user makes a changes to any part of the Date/Time in the DateTimePicker control. There is no need for this to be a cancelable event type as there is nothing to cancel.

In the Validating event you are canceling the change of focus from one control to another and therfore preventing the user moving away from a control with an invalid value.

Which event you choose to use is up to you and your personal preference for the use and feel to your program.

Nick.

Sorry, I did not understand your problem fully before.

The problem is that the DateTimePicker is returning the date at midnight (i.e. with a time of 00:00:00) and DateTime.Now is constantly updating to the current time.

Using DateTime.Today instead of DateTime.Now is both places in your code should solve it.

Nick.

there is an error sill the message box is interating more than that ??? hwat shoul i do ???

I just tried this and you're right.
It seems that setting the DateTimePicker Value in the ValueChanged event causes it to trigger the event again with the original value.
Not sure how to get around this as I have not seen it before.
I did this to see what was happening.

int scanCounter = 0;
        private void dpStartD_ValueChanged(object sender, EventArgs e)
        {
            int thisScan = scanCounter++;
            System.Diagnostics.Debug.WriteLine(string.Format("{0} ValueChanged start", thisScan));
            if (dpStartD.Value < DateTime.Today)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("{0} ValueChanged Error before set", thisScan));
                dpStartD.Value = DateTime.Today;
                System.Diagnostics.Debug.WriteLine(string.Format("{0} ValueChanged Error after set", thisScan));
                System.Windows.Forms.MessageBox.Show("Invalid Session Start Date");
                System.Diagnostics.Debug.WriteLine(string.Format("{0} ValueChanged Error after message", thisScan));
            }
            System.Diagnostics.Debug.WriteLine(string.Format("{0} ValueChanged end", thisScan));
        }

It is the end of my day now so I'll have to leave you to look at this on your own.
If you find a solution post it please.
Nick.

I found out that this is already a known issue and several other forums have discussed this problem.

One solution is to set the minimum value in the form constructor.
i.e. dpStartD.MinDate = DateTime.Today;
This prevents the user from selecting an invalid date in the first place.

Another is to use the DateTimepicker.CloseUp event instead of the ValueChanged event.

private void dpStartD_CloseUp(object sender, EventArgs e)
{
    if (dpStartD.Value < DateTime.Today)
    {
        dpStartD.Value = DateTime.Today;
        System.Windows.Forms.MessageBox.Show("Invalid Session Start Date");
    }
}

Let me know if this works.

hello people,
My question is, How do I disable future dates (i.e.tomorrow,day after tomorrow e.t.c.) in my windows form application such that the user cannot select them. This should be done automatically in that if the day changes,the sequence continues

This article has been dead for over six months. Start a new discussion instead.