hi there,

i have two datetime picker in the form that i have created. i added the below code for the datetime picker.one dpSDate and the other dpEDate.

private void dpStartD_Validating(object sender, CancelEventArgs e)
        {
            if (dpStartD.Value < System.DateTime.Now.Date)
            {
                dpStartD.Value = System.DateTime.Now;
                MessageBox.Show("Enter a valid date for start date");
            }
        }

        private void dpEndD_Validating(object sender, CancelEventArgs e)
        {
            if (dpEndD.Value > System.DateTime.Now.Date)
            {
                dpStartD.Value = System.DateTime.Now;
                MessageBox.Show("Enter a valid date end date");
            }
            if (dpStartD.Value > dpEndD.Value)
            {
                dpStartD.Value = System.DateTime.Now;
                MessageBox.Show("End date should be greater than the start date");
            }
        }

when i click the date from the datetimepicker and click the date that was a previous date it should give me and error after i click and finish.

but when i click the date the error msg dosen't come as soon i have finish clicking the date. i have to click some where else to display the error msg

how can i change the code so that when i finish selecting a value from the datetime picker the error msg to display

how can i do this

thanx

You may need to play around with the events of Value changed for each of the datetimepicker. This will trigger as soon as you make the change..

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (dateTimePicker1.Value > dateTimePicker2.Value)
            {
                MessageBox.Show("You Can not select a larger start date");
            }
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            if (dateTimePicker1.Value < dateTimePicker2.Value)
            {
                MessageBox.Show("You Can not select a larger end date");
            }
        }

you could validate on ValueChanged of DateTimePicker. Simply go to your designer and add an event ValueChanged in the datetimepicker.

You may need to play around with the events of Value changed for each of the datetimepicker. This will trigger as soon as you make the change..

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (dateTimePicker1.Value > dateTimePicker2.Value)
            {
                MessageBox.Show("You Can not select a larger start date");
            }
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            if (dateTimePicker1.Value < dateTimePicker2.Value)
            {
                MessageBox.Show("You Can not select a larger end date");
            }
        }

hey thankxx

i have the below code for validating the datetime picker. why is this code running two times??

any idea??

private void dpStartD_ValueChanged(object sender, EventArgs e)
        {
            if (dpStartD.Value < System.DateTime.Now.Date)
            {
                MessageBox.Show("Enter a valid date for start date");
            } 
            dpStartD.Text = DateTime.Today.ToString();
            return;
        }

thankx

because you are changing the value on line 7 that triggers the value changed event again. Initially when the form starts the value of the datetimepicker is the current date time. by the time you change it after the code has ran the time is different. so once again it triggers the value changed event.

because you are changing the value on line 7 that triggers the value changed event again. Initially when the form starts the value of the datetimepicker is the current date time. by the time you change it after the code has ran the time is different. so once again it triggers the value changed event.

so how can i avoid it

don't compare against datetime.now compare against the other datetimepicker you have. that way it will only show it once.

don't compare against datetime.now compare against the other datetimepicker you have. that way it will only show it once.

hey i want to compare that also
but how can i compare if the user enters a date that is in the past,
it should not allow the user to add a date that have already passed

how can i validate this

thanxxxx

This may not be the best way of getting around the double message but it works. another method you may want to try perhaps a Form Label. It works and I'm sure there is another way to get around the double prompt... just havent figured it out yet.. Basically a counter that counts on the amount of times the prompts comes up. and then just resets after showing the message.

int counter;
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {


            if ((dateTimePicker1.Value.Date < DateTime.Now.Date))
            {
                counter++;
                dateTimePicker1.ResetText();
                if (counter == 2)
                {
                    MessageBox.Show("You can not choose an older date than the current date" + dateTimePicker1.Value.Date);
                    counter = 0;
                }

            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.