WinForm练习--日期选择器

namespace _12日期选择器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    //获取当前时间
    int year = DateTime.Now.Year;
    //当程序运行时就将年份加载进去cboYears
    for (int i = year; i >=1949; i--)
    {
        cboYears.Items.Add(i+"年");
    }
}

private void cboYears_SelectedIndexChanged(object sender, EventArgs e)
{
    //添加月份之前清空之前记录
    cboMonths.Items.Clear();
    //当选择年份时,加载月份
    for (int i = 1; i <= 12; i++)
    {
        cboMonths.Items.Add(i+"月");
    }
}

private void cboMonths_SelectedIndexChanged(object sender, EventArgs e)
{
    //定义一个day来存储天数
    int day = 0;
    //加载day之前清空之前记录
    cboDays.Items.Clear();
    //获得月份
    string strmonth = cboMonths.SelectedItem.ToString().Split(new char[] { '月' }, StringSplitOptions.RemoveEmptyEntries)[0];
    //获得年份
    string strYear = cboYears.SelectedItem.ToString().Split(new char[] { '年' }, StringSplitOptions.RemoveEmptyEntries)[0];
    //将年月字符串类型转换成int类型
    int year = Convert.ToInt32(strYear);
    int month = Convert.ToInt32(strmonth);
    //对month进行多条件判断
    switch (month)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:day = 31;
            break;
        case 2:if(year%400==0 || year%4==0 && year % 100 != 0)
            {
                day = 29;
            }
            else
            {
                day = 28;
            }
            break;
        default:day = 30;
            break;
    }
    for (int i = 1; i <= day; i++)
    {
        cboDays.Items.Add(i+"日");
    }
}

}
}
东莞网站建设www.zg886.cn

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值