Click here to Skip to main content
15,923,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi In my Windows Form Application there is two columns viz; DOB for which I used three comboBoxes as "dd(comboBox9),mm(comboBox10),yyyy(comboBox11)" and Date of Retirement(textBox3). The DOB entered by the user should automatically show the user's Date of Retirement by adding 60 years to his/her DOB. I have writtem some codes for this operation as
C#
private void comboBox11_SelectedIndexChanged(object sender, EventArgs e)
        {
            DateTime dt = new DateTime();
            //dt = ;
            comboBox9.Text = dt.Day.ToString();
            comboBox10.Text = dt.Month.ToString();
            comboBox11.Text = dt.Year.ToString();

                dt.AddYears(60);
                textBox3.Text = dt.ToString();


        }

By entering the DOB, it shows 01-Jan-01 12:00:00 AM in the Date of Retirement column for every DOB and also changes the DOB column to 1 1 2000. I am unable to find out suitable reason. Please someone help me.
Posted

Please check this.

C#
DateTime dt = new DateTime(Convert.ToInt32(comboBox11.Text), Convert.ToInt32(comboBox10.Text), Convert.ToInt32(comboBox9.Text));
DateTime dt1 = dt.AddYears(60);
textBox3.Text = dt1.ToShortDateString();
 
Share this answer
 
v2
The reason for that is you are doing a new on datetime and never using the user selected date. You are just using the default date.

so try doing this instead.

C#
DateTime dt = new DateTime(Convert.ToInt32(comboBox11.Text), Convert.ToInt32(comboBox10.Text), Convert.ToInt32(comboBox9.Text));


               dt.AddYears(60);
               textBox3.Text = dt.ToString();


This should fix the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900