• 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;
            }
        }
    }
  • 相关阅读:
    [BZOJ2324][ZJOI2011]营救皮卡丘
    P4324 [JSOI2016]扭动的回文串
    P5068 [Ynoi2015]我回来了
    P4412 [SHOI2004]最小生成树
    bzoj3118: Orz the MST(线性规划+单纯形法)
    bzoj3265: 志愿者招募加强版(线性规划+单纯形法)
    bzoj3550: [ONTAK2010]Vacation(单纯形法+线性规划)
    uoj#179. 线性规划
    P2093 [国家集训队]JZPFAR(KDTree)
    P3538 [POI2012]OKR-A Horrible Poem
  • 原文地址:https://www.cnblogs.com/zhaoxianglong1987/p/7619953.html
Copyright © 2020-2023  润新知