码迷,mamicode.com
首页 > Web开发 > 详细

.Net学习笔记----2015-07-02(日期选择器)

时间:2015-07-02 13:43:45      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 当年份发生改变的时候 加载月份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //添加之前应该清空之前的内容
            cboMonths.Items.Clear();
            for (int i = 1; i <= 12; i++)
            {
                cboMonths.Items.Add(i + "");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //程序加载时,将年份加到下拉框中
            //获得当前年份
            int year = DateTime.Now.Year;
            for (int i = year; i >= 1949; i--)
            {
                cboYears.Items.Add(i + "");
            }
        }
        /// <summary>
        /// 当选中月份时    加载日期
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboMonths_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboDays.Items.Clear();
            int day = 0;
            //获得月份            
            string strMonth = cboMonths.SelectedItem.ToString().Split(new char[] {  })[0];
            string strYear = cboYears.SelectedItem.ToString().Split(new char[] {  })[0];

            int year = Convert.ToInt32(strYear);
            int month = Convert.ToInt32(strMonth);

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




        }
    }

 

.Net学习笔记----2015-07-02(日期选择器)

标签:

原文地址:http://www.cnblogs.com/mikie/p/4615717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!