12.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.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
网页标题:12.WinForm练习--日期选择器-创新互联
URL链接:http://ybzwz.com/article/gogod.html