在datetimepicker中更改年份 [英] Change year in datetimepicker

查看:91
本文介绍了在datetimepicker中更改年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

试试这个:
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);
        }
    }


这篇关于在datetimepicker中更改年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆