利用TreeView生成日期树 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Date { public partial class Form1 : Form { static string strMonth; private string Month(int i) { switch (i) { case 1: strMonth = "一月"; break; case 2: strMonth = "二月"; break; case 3: strMonth = "三月"; break; case 4: strMonth = "四月"; break; case 5: strMonth = "五月"; break; case 6: strMonth = "六月"; break; case 7: strMonth = "七月"; break; case 8: strMonth = "八月"; break; case 9: strMonth = "九月"; break; case 10: strMonth = "十月"; break; case 11: strMonth = "十一月"; break; case 12: strMonth = "十二月"; break; } return strMonth; } bool Leapyear(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return true; } return false; } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MessageBox.Show("hehe"); treeView1.ShowLines = true; TreeNode newNode0 = treeView1.Nodes.Add("日期"); for (int i = 1; i <= 12; i++) { string strMon = Month(i); treeView1.Nodes[0].Nodes.Add(strMon); } int days = 0;//二月份天数的判断 int thisYear = System.DateTime.Now.Year; if (Leapyear(thisYear)) { days = 29; } else { days = 28; } for (int i = 1; i <= 12; i++) { if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) { for (int j = 1; j <= 31; j++) { treeView1.Nodes[0].Nodes[i - 1].Nodes.Add(j.ToString()); } } else if (i == 2) { for (int j = 1; j <= days; j++) { treeView1.Nodes[0].Nodes[i - 1].Nodes.Add(j.ToString()); } } else { for (int j = 1; j <= 30; j++) { treeView1.Nodes[0].Nodes[i - 1].Nodes.Add(j.ToString()); } } } newNode0.Expand(); } } } 如图如示: