locked
How to get 2 datetimepicker controllers's difference in year, month and days in C#? Why my messagebox is appearing mone than once? RRS feed

  • Question

  • Dear all,

    I have designed a form with two datetimepicker so that changing anyone, the labels should show the difference in year, month and days. I have made it manually but in some cases it is giving some error. Can anyone solve my problem with built-in function directly?

    Secondly, I have prepared a message box for avoiding negative result. It is coming but it is appearing 2/3 times consecutively and then minimizing the main program! Can anyone solve it for me?

    Thanks everyone.

    My coding is as follows:

            private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
            {
                if (dateTimePicker2.Value > dateTimePicker1.Value)
                {
                    TimeSpan age;
                    age = dateTimePicker2.Value - dateTimePicker1.Value;
                    int year;
                    int month;
                    int day;
                    year = age.Days / 365;
                    month = (age.Days % 365) / 30;
                    day = (age.Days % 365) % 30;
                    label7.Text = year.ToString();
                    label8.Text = month.ToString();
                    label9.Text = day.ToString();
                }
                else
                {
                    label7.Text = label8.Text = label9.Text = "0";
                    dateTimePicker1.Value = dateTimePicker2.Value = System.DateTime.Now;
                    MessageBox.Show("From date must be earlier than To date!",
                        "Error!",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation,
                        MessageBoxDefaultButton.Button1);
                }
            }

    • Moved by CoolDadTx Thursday, May 2, 2013 1:49 PM Winforms related
    Thursday, May 2, 2013 12:56 PM

Answers

  • Razib,

    your problem occurs because the validation happens as the date changes and then will keep happening as long as date2 is prior to date1

    Simply change your validation to this:

            private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
            {
                if (dateTimePicker2.Value < dateTimePicker1.Value)
                {
                    MessageBox.Show("The end date is before the start date");
                    dateTimePicker2.Value = dateTimePicker1.Value;
                    return;
                }
            }
    

    The user will be prevented form entering an invalid timespan and all your other code will work.

    Regards

    Rupert


    the problem is not what you don't know it's what you think you know that's wrong

    • Proposed as answer by Mike Feng Friday, May 3, 2013 8:58 AM
    • Marked as answer by Mike Feng Sunday, May 12, 2013 8:37 AM
    Thursday, May 2, 2013 1:17 PM

All replies

  • Razib,

    your problem occurs because the validation happens as the date changes and then will keep happening as long as date2 is prior to date1

    Simply change your validation to this:

            private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
            {
                if (dateTimePicker2.Value < dateTimePicker1.Value)
                {
                    MessageBox.Show("The end date is before the start date");
                    dateTimePicker2.Value = dateTimePicker1.Value;
                    return;
                }
            }
    

    The user will be prevented form entering an invalid timespan and all your other code will work.

    Regards

    Rupert


    the problem is not what you don't know it's what you think you know that's wrong

    • Proposed as answer by Mike Feng Friday, May 3, 2013 8:58 AM
    • Marked as answer by Mike Feng Sunday, May 12, 2013 8:37 AM
    Thursday, May 2, 2013 1:17 PM
  • p.s. Always ensure that both datetimepickers are set to a valid date when the form is shown! 

    the problem is not what you don't know it's what you think you know that's wrong

    Thursday, May 2, 2013 1:18 PM
  • Hello Razib007,

    else part

    you write :

     --> dateTimePicker1.Value = dateTimePicker2.Value = System.DateTime.Now;

    so if your code enters else part, dateTimePicker1_ValueChanged event 3 times works.

    You should write this another event.

    Good days...



    • Edited by hobbit1422 Friday, May 3, 2013 9:38 AM
    • Proposed as answer by hobbit1422 Saturday, May 4, 2013 12:56 PM
    • Unproposed as answer by hobbit1422 Saturday, May 4, 2013 12:56 PM
    Thursday, May 2, 2013 1:31 PM