Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

here i want change year in datetime picker based on year, the total 12 year is available on another ComboBox control, so which year user choose i want to change that year in datetimepicker(windows application)

THANKS


What I have tried:

DateTime YearSelect(int Year)
        {
           return DateTime.Now.AddYears(-DateTime.Now.year).AddYears(Year+1);
        }

   private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            dateTimePicker1.Value=YearSelect(Convert.ToInt32(comboBox3.SelectedItem.Text));
        }
Posted
Updated 11-Feb-19 19:49pm

1 solution

Try this:
private void cbSelectYear_SelectedIndexChanged(object sender, EventArgs e)
    {
    int year;
    if (int.TryParse(cbSelectYear.Text, out year))
        {
        DateTime dt = myDateTimePicker.Value;
        myDateTimePicker.Value = new DateTime(year, dt.Month, dt.Day);
        }
    }
 
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