计算闰年_winform

新建窗体应用程序(如下),新建控件label1,label2,label3,textBOX1,button1,button2

label1的Text属性改为“计算闰年演示”

label2的Text属性改为“输入年份”

button1的Text属性改为“确定”

button1的Text属性改为“退出”

完整代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace 计算闰年_窗体
12 {
13     public partial class Form1 : Form
14     {
15         //闰年:能被4整除,但不能被100整除或者能被400整除的年份
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void button2_Click(object sender, EventArgs e)//退出按钮代码
22         {
23             Application.Exit();
24         }
25 
26         private void button1_Click(object sender, EventArgs e)//确定按钮代码
27         {
28             try
29             {
30                 int year = Convert.ToInt32(textBox1.Text);
31                 if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)//闰年:能被4整除,但不能被100整除或者能被400整除的年份
32                 {
33                     label3.Text = string.Format("{0}是闰年", year);//输出显示闰年
34                 }
35                 else
36                 {
37                     label3.Text = string.Format("{0}不是闰年", year);//输出显示非闰年
38                 }
39             }
40             catch
41             {
42                 MessageBox.Show("请输入正确年份");
43             }
44         }
45     }
46 }
原文地址:https://www.cnblogs.com/start-from-scratch/p/5046524.html