zoukankan      html  css  js  c++  java
  • c#选择日期

    using System;
    using System.Windows.Forms;
    
    namespace DateDemo
    {
        public partial class Form1 : Form
        {
            private int _currentYearIndex;
            private int _currentMonthIndex;
            private const int StartYear = 1949;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DateTime today = DateTime.Today;
                for (var i = today.Year; i >=StartYear; i--)
                {
                    cbYear.Items.Add(i);
                }
                for (var i = 1; i < 13; i++)
                {
                    cbMonth.Items.Add(i);
                }
    
                for (var i = 1; i < GetDays(today.Year, today.Month); i++)
                {
                    cbDay.Items.Add(i);
                }
    
                cbYear.SelectedIndex = 0;
                cbMonth.SelectedIndex = today.Month - 1;
                cbDay.SelectedIndex = today.Day - 1;
            }
    
            private int GetDays(int year, int month)
            {
                var days = 0;
                switch (month)
                {
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        days = 30;
                        break;
    
                    case 2:
                        days = IsLeapYear(year) ? 29 : 28;
                        break;
    
                    default:
                        days = 31;
                        break;
                }
    
                return days;
            }
    
            public static bool IsLeapYear(int year)
            {
                return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
            }
    
            private void cbYear_SelectedIndexChanged(object sender, EventArgs e)
            {
                _currentYearIndex = cbYear.SelectedIndex;
                int y = DateTime.Today.Year- cbYear.SelectedIndex;
                int m = _currentMonthIndex + 1;
                //            MessageBox.Show(y+"  "+m);
                SettingCbDay(y, m);
                cbDay.SelectedIndex = 0;
            }
    
            private void SettingCbDay(int y, int m)
            {
                cbDay.Items.Clear();
                for (var i = 1; i <= GetDays(y, m); i++)
                {
                    cbDay.Items.Add(i);
                }
            }
    
            private void cbMonth_SelectedIndexChanged(object sender, EventArgs e)
            {
                _currentMonthIndex = cbMonth.SelectedIndex;
                int y = DateTime.Today.Year - cbYear.SelectedIndex;
                int m = cbMonth.SelectedIndex + 1;
                //            MessageBox.Show(y + "  " + m);
                SettingCbDay(y, m);
                cbDay.SelectedIndex = 0;
            }
        }
    }
  • 相关阅读:
    php基础
    MYSQL 常用函数
    MYSQL 练习题
    MYSQL 查询
    MYSQL:增删改
    隐藏导航
    分层导航
    图片轮播!
    你帅不帅?
    PHP 流程
  • 原文地址:https://www.cnblogs.com/zhaoxianglong1987/p/7619953.html
Copyright © 2011-2022 走看看