Click here to Skip to main content
13,863,229 members
Rate this:
 
Please Sign up or sign in to vote.
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 20:49pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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);
        }
    }
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190214.1 | Last Updated 12 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100