码迷,mamicode.com
首页 > 其他好文 > 详细

Combobox的使用,日期选择器

时间:2017-08-23 11:56:29      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:选择器   tar   init   data   lin   pre   end   down   选择   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            int year = DateTime.Now.Year;

            for (int i = 0; i <= 20; i++ )
            {
                cboYear.Items.Add((year - i)+"年");
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            cboYear.Items.Remove("2");
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            cboYear.Items.Clear();
        }

        private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboMonth.Items.Count != 0)
                return;
            for(int i = 1; i <= 12; i++)
            {
                cboMonth.Items.Add(i + "月");
            }
        }

        private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboDay.Items.Clear();
            int day = 0;

            //cboMonth.SelectedItem.ToString() 返回1月,要去除月
            //Split(new char[] {‘月‘},StringSplitOptions.RemoveEmptyEntries) 返回数组
            string strMonth = cboMonth.SelectedItem.ToString().Split(new char[] {‘月‘},StringSplitOptions.RemoveEmptyEntries)[0];
            string strYear = cboYear.SelectedItem.ToString().Split(new char[] {‘年‘},StringSplitOptions.RemoveEmptyEntries)[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++)
            {
                cboDay.Items.Add(i + "日");
            }
        }
    }
}

重要属性DropDownStyle  

Combobox的使用,日期选择器

标签:选择器   tar   init   data   lin   pre   end   down   选择   

原文地址:http://www.cnblogs.com/my-cat/p/7417159.html

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