C#做的一個日期控件,用控件數組做的
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CustTool
{
/// <summary>
/// Form1 的摘要描述。
/// </summary>
public class Calendar : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnprev;
private System.Windows.Forms.Button btnnext;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label lblmonth;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.Button button12;
private System.Windows.Forms.Button button13;
private System.Windows.Forms.Button button14;
private System.Windows.Forms.Button button15;
private System.Windows.Forms.Button button16;
private System.Windows.Forms.Button button17;
private System.Windows.Forms.Button button18;
private System.Windows.Forms.Button button19;
private System.Windows.Forms.Button button20;
private System.Windows.Forms.Button button21;
private System.Windows.Forms.Button button22;
private System.Windows.Forms.Button button23;
private System.Windows.Forms.Button button24;
private System.Windows.Forms.Button button25;
private System.Windows.Forms.Button button26;
private System.Windows.Forms.Button button27;
private System.Windows.Forms.Button button28;
private System.Windows.Forms.Button button29;
private System.Windows.Forms.Button button30;
private System.Windows.Forms.Button button31;
private System.Windows.Forms.Button button32;
private System.Windows.Forms.Button button33;
private System.Windows.Forms.Button button34;
private System.Windows.Forms.Button button35;
private System.Windows.Forms.Button button36;
private System.Windows.Forms.Button button37;
private System.Windows.Forms.Button button38;
private System.Windows.Forms.Button button39;
private System.Windows.Forms.Button button40;
private System.Windows.Forms.Button button41;
private System.Windows.Forms.Button button42;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
/// <summary>
/// 設計工具所需的變數。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label today;
private int year;
private int month;
private int day;
private Button[] myButton = new Button[42];
public Calendar()
{
//
// Windows Form 設計工具支援的必要項
//
InitializeComponent();
this.btnprev.Click += new System.EventHandler(this.btnprev_Click);
this.btnnext.Click += new System.EventHandler(this.btnnext_Click);
myButton[0] = button1;
myButton[1] = button2;
myButton[2] = button3;
myButton[3] = button4;
myButton[4] = button5;
myButton[5] = button6;
myButton[6] = button7;
myButton[7] = button8;
myButton[8] = button9;
myButton[9] = button10;
myButton[10] = button11;
myButton[11] = button12;
myButton[12] = button13;
myButton[13] = button14;
myButton[14] = button15;
myButton[15] = button16;
myButton[16] = button17;
myButton[17] = button18;
myButton[18] = button19;
myButton[19] = button20;
myButton[20] = button21;
myButton[21] = button22;
myButton[22] = button23;
myButton[23] = button24;
myButton[24] = button25;
myButton[25] = button26;
myButton[26] = button27;
myButton[27] = button28;
myButton[28] = button29;
myButton[29] = button30;
myButton[30] = button31;
myButton[31] = button32;
myButton[32] = button33;
myButton[33] = button34;
myButton[34] = button35;
myButton[35] = button36;
myButton[36] = button37;
myButton[37] = button38;
myButton[38] = button39;
myButton[39] = button40;
myButton[40] = button41;
myButton[41] = button42;
//
// TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
//
}
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form 設計工具產生的程式碼
/// <summary>
/// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
/// 這個方法的內容。
/// </summary>
private void InitializeComponent()
{
this.btnprev = new System.Windows.Forms.Button();
this.btnnext = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.lblmonth = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.button16 = new System.Windows.Forms.Button();
this.button17 = new System.Windows.Forms.Button();
this.button18 = new System.Windows.Forms.Button();
this.button19 = new System.Windows.Forms.Button();
this.button20 = new System.Windows.Forms.Button();
this.button21 = new System.Windows.Forms.Button();
this.button22 = new System.Windows.Forms.Button();
this.button23 = new System.Windows.Forms.Button();
this.button24 = new System.Windows.Forms.Button();
this.button25 = new System.Windows.Forms.Button();
this.button26 = new System.Windows.Forms.Button();
this.button27 = new System.Windows.Forms.Button();
this.button28 = new System.Windows.Forms.Button();
this.button29 = new System.Windows.Forms.Button();
this.button30 = new System.Windows.Forms.Button();
this.button31 = new System.Windows.Forms.Button();
this.button32 = new System.Windows.Forms.Button();
this.button33 = new System.Windows.Forms.Button();
this.button34 = new System.Windows.Forms.Button();
this.button35 = new System.Windows.Forms.Button();
this.button36 = new System.Windows.Forms.Button();
this.button37 = new System.Windows.Forms.Button();
this.button38 = new System.Windows.Forms.Button();
this.button39 = new System.Windows.Forms.Button();
this.button40 = new System.Windows.Forms.Button();
this.button41 = new System.Windows.Forms.Button();
this.button42 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.today = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnprev
//
this.btnprev.Location = new System.Drawing.Point(16, 48);
this.btnprev.Name = "btnprev";
this.btnprev.Size = new System.Drawing.Size(32, 23);
this.btnprev.TabIndex = 3;
this.btnprev.Text = "<<";
//
// btnnext
//
this.btnnext.Location = new System.Drawing.Point(488, 48);
this.btnnext.Name = "btnnext";
this.btnnext.Size = new System.Drawing.Size(32, 23);
this.btnnext.TabIndex = 9;
this.btnnext.Text = ">>";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 120);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 40);
this.button1.TabIndex = 10;
this.button1.Text = "button1";
//
// lblmonth
//
this.lblmonth.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.lblmonth.Location = new System.Drawing.Point(48, 48);
this.lblmonth.Name = "lblmonth";
this.lblmonth.Size = new System.Drawing.Size(440, 23);
this.lblmonth.TabIndex = 11;
this.lblmonth.Text = "月份";
this.lblmonth.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// button2
//
this.button2.Location = new System.Drawing.Point(83, 120);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 40);
this.button2.TabIndex = 12;
this.button2.Text = "button2";
//
// button3
//
this.button3.Location = new System.Drawing.Point(158, 120);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 40);
this.button3.TabIndex = 13;
this.button3.Text = "button3";
//
// button4
//
this.button4.Location = new System.Drawing.Point(233, 120);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 40);
this.button4.TabIndex = 14;
this.button4.Text = "button4";
//
// button5
//
this.button5.Location = new System.Drawing.Point(308, 120);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 40);
this.button5.TabIndex = 15;
this.button5.Text = "button5";
//
// button6
//
this.button6.Location = new System.Drawing.Point(383, 120);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 40);
this.button6.TabIndex = 16;
this.button6.Text = "button6";
//
// button7
//
this.button7.BackColor = System.Drawing.SystemColors.Control;
this.button7.Location = new System.Drawing.Point(458, 120);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(75, 40);
this.button7.TabIndex = 17;
this.button7.Text = "button7";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 88);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 23);
this.label1.TabIndex = 18;
this.label1.Text = "星期日";
//
// label2
//
this.label2.Location = new System.Drawing.Point(92, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 23);
this.label2.TabIndex = 19;
this.label2.Text = "星期一";
//
// button8
//
this.button8.Location = new System.Drawing.Point(8, 160);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(75, 40);
this.button8.TabIndex = 20;
this.button8.Text = "button8";
//
// button9
//
this.button9.Location = new System.Drawing.Point(83, 160);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(75, 40);
this.button9.TabIndex = 21;
this.button9.Text = "button9";
//
// button10
//
this.button10.Location = new System.Drawing.Point(158, 160);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(75, 40);
this.button10.TabIndex = 22;
this.button10.Text = "button10";
//
// button11
//
this.button11.Location = new System.Drawing.Point(233, 160);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(75, 40);
this.button11.TabIndex = 23;
this.button11.Text = "button11";
//
// button12
//
this.button12.Location = new System.Drawing.Point(308, 160);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(75, 40);
this.button12.TabIndex = 24;
this.button12.Text = "button12";
//
// button13
//
this.button13.Location = new System.Drawing.Point(383, 160);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(75, 40);
this.button13.TabIndex = 25;
this.button13.Text = "button13";
//
// button14
//
this.button14.BackColor = System.Drawing.SystemColors.Control;
this.button14.Location = new System.Drawing.Point(458, 160);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(75, 40);
this.button14.TabIndex = 26;
this.button14.Text = "button14";
//
// button15
//
this.button15.Location = new System.Drawing.Point(8, 200);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(75, 40);
this.button15.TabIndex = 27;
this.button15.Text = "button15";
//
// button16
//
this.button16.Location = new System.Drawing.Point(83, 200);
this.button16.Name = "button16";
this.button16.Size = new System.Drawing.Size(75, 40);
this.button16.TabIndex = 28;
this.button16.Text = "button16";
//
// button17
//
this.button17.Location = new System.Drawing.Point(158, 200);
this.button17.Name = "button17";
this.button17.Size = new System.Drawing.Size(75, 40);
this.button17.TabIndex = 29;
this.button17.Text = "button17";
//
// button18
//
this.button18.Location = new System.Drawing.Point(233, 200);
this.button18.Name = "button18";
this.button18.Size = new System.Drawing.Size(75, 40);
this.button18.TabIndex = 30;
this.button18.Text = "button18";
//
// button19
//
this.button19.Location = new System.Drawing.Point(308, 200);
this.button19.Name = "button19";
this.button19.Size = new System.Drawing.Size(75, 40);
this.button19.TabIndex = 31;
this.button19.Text = "button19";
//
// button20
//
this.button20.Location = new System.Drawing.Point(383, 200);
this.button20.Name = "button20";
this.button20.Size = new System.Drawing.Size(75, 40);
this.button20.TabIndex = 32;
this.button20.Text = "button20";
//
// button21
//
this.button21.Location = new System.Drawing.Point(458, 200);
this.button21.Name = "button21";
this.button21.Size = new System.Drawing.Size(75, 40);
this.button21.TabIndex = 33;
this.button21.Text = "button21";
//
// button22
//
this.button22.Location = new System.Drawing.Point(8, 240);
this.button22.Name = "button22";
this.button22.Size = new System.Drawing.Size(75, 40);
this.button22.TabIndex = 34;
this.button22.Text = "button22";
//
// button23
//
this.button23.Location = new System.Drawing.Point(83, 240);
this.button23.Name = "button23";
this.button23.Size = new System.Drawing.Size(75, 40);
this.button23.TabIndex = 35;
this.button23.Text = "button23";
//
// button24
//
this.button24.Location = new System.Drawing.Point(158, 240);
this.button24.Name = "button24";
this.button24.Size = new System.Drawing.Size(75, 40);
this.button24.TabIndex = 36;
this.button24.Text = "button24";
//
// button25
//
this.button25.Location = new System.Drawing.Point(233, 240);
this.button25.Name = "button25";
this.button25.Size = new System.Drawing.Size(75, 40);
this.button25.TabIndex = 37;
this.button25.Text = "button25";
//
// button26
//
this.button26.Location = new System.Drawing.Point(308, 240);
this.button26.Name = "button26";
this.button26.Size = new System.Drawing.Size(75, 40);
this.button26.TabIndex = 38;
this.button26.Text = "button26";
//
// button27
//
this.button27.Location = new System.Drawing.Point(383, 240);
this.button27.Name = "button27";
this.button27.Size = new System.Drawing.Size(75, 40);
this.button27.TabIndex = 39;
this.button27.Text = "button27";
//
// button28
//
this.button28.Location = new System.Drawing.Point(458, 240);
this.button28.Name = "button28";
this.button28.Size = new System.Drawing.Size(75, 40);
this.button28.TabIndex = 40;
this.button28.Text = "button28";
//
// button29
//
this.button29.Location = new System.Drawing.Point(8, 280);
this.button29.Name = "button29";
this.button29.Size = new System.Drawing.Size(75, 40);
this.button29.TabIndex = 41;
this.button29.Text = "button29";
//
// button30
//
this.button30.Location = new System.Drawing.Point(83, 280);
this.button30.Name = "button30";
this.button30.Size = new System.Drawing.Size(75, 40);
this.button30.TabIndex = 42;
this.button30.Text = "button30";
//
// button31
//
this.button31.Location = new System.Drawing.Point(158, 280);
this.button31.Name = "button31";
this.button31.Size = new System.Drawing.Size(75, 40);
this.button31.TabIndex = 43;
this.button31.Text = "button31";
//
// button32
//
this.button32.Location = new System.Drawing.Point(233, 280);
this.button32.Name = "button32";
this.button32.Size = new System.Drawing.Size(75, 40);
this.button32.TabIndex = 44;
this.button32.Text = "button32";
//
// button33
//
this.button33.Location = new System.Drawing.Point(308, 280);
this.button33.Name = "button33";
this.button33.Size = new System.Drawing.Size(75, 40);
this.button33.TabIndex = 45;
this.button33.Text = "button33";
//
// button34
//
this.button34.Location = new System.Drawing.Point(383, 280);
this.button34.Name = "button34";
this.button34.Size = new System.Drawing.Size(75, 40);
this.button34.TabIndex = 46;
this.button34.Text = "button34";
//
// button35
//
this.button35.Location = new System.Drawing.Point(458, 280);
this.button35.Name = "button35";
this.button35.Size = new System.Drawing.Size(75, 40);
this.button35.TabIndex = 47;
this.button35.Text = "button35";
//
// button36
//
this.button36.Location = new System.Drawing.Point(8, 320);
this.button36.Name = "button36";
this.button36.Size = new System.Drawing.Size(75, 40);
this.button36.TabIndex = 48;
this.button36.Text = "button36";
//
// button37
//
this.button37.Location = new System.Drawing.Point(83, 320);
this.button37.Name = "button37";
this.button37.Size = new System.Drawing.Size(75, 40);
this.button37.TabIndex = 49;
this.button37.Text = "button37";
//
// button38
//
this.button38.Location = new System.Drawing.Point(158, 320);
this.button38.Name = "button38";
this.button38.Size = new System.Drawing.Size(75, 40);
this.button38.TabIndex = 50;
this.button38.Text = "button38";
//
// button39
//
this.button39.Location = new System.Drawing.Point(233, 320);
this.button39.Name = "button39";
this.button39.Size = new System.Drawing.Size(75, 40);
this.button39.TabIndex = 51;
this.button39.Text = "button39";
//
// button40
//
this.button40.Location = new System.Drawing.Point(308, 320);
this.button40.Name = "button40";
this.button40.Size = new System.Drawing.Size(75, 40);
this.button40.TabIndex = 52;
this.button40.Text = "button40";
//
// button41
//
this.button41.Location = new System.Drawing.Point(383, 320);
this.button41.Name = "button41";
this.button41.Size = new System.Drawing.Size(75, 40);
this.button41.TabIndex = 53;
this.button41.Text = "button41";
//
// button42
//
this.button42.Location = new System.Drawing.Point(458, 320);
this.button42.Name = "button42";
this.button42.Size = new System.Drawing.Size(75, 40);
this.button42.TabIndex = 54;
this.button42.Text = "button42";
//
// label3
//
this.label3.Location = new System.Drawing.Point(168, 88);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(48, 23);
this.label3.TabIndex = 55;
this.label3.Text = "星期二";
//
// label4
//
this.label4.Location = new System.Drawing.Point(244, 88);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(48, 23);
this.label4.TabIndex = 56;
this.label4.Text = "星期三";
//
// label5
//
this.label5.Location = new System.Drawing.Point(320, 88);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(48, 23);
this.label5.TabIndex = 57;
this.label5.Text = "星期四";
//
// label6
//
this.label6.Location = new System.Drawing.Point(396, 88);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(48, 23);
this.label6.TabIndex = 58;
this.label6.Text = "星期五";
//
// label7
//
this.label7.Location = new System.Drawing.Point(472, 88);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(48, 23);
this.label7.TabIndex = 59;
this.label7.Text = "星期六";
//
// today
//
this.today.Location = new System.Drawing.Point(48, 376);
this.today.Name = "today";
this.today.Size = new System.Drawing.Size(280, 23);
this.today.TabIndex = 60;
this.today.Text = "today";
//
// Calendar
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(552, 405);
this.Controls.Add(this.today);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.button42);
this.Controls.Add(this.button41);
this.Controls.Add(this.button40);
this.Controls.Add(this.button39);
this.Controls.Add(this.button38);
this.Controls.Add(this.button37);
this.Controls.Add(this.button36);
this.Controls.Add(this.button35);
this.Controls.Add(this.button34);
this.Controls.Add(this.button33);
this.Controls.Add(this.button32);
this.Controls.Add(this.button31);
this.Controls.Add(this.button30);
this.Controls.Add(this.button29);
this.Controls.Add(this.button28);
this.Controls.Add(this.button27);
this.Controls.Add(this.button26);
this.Controls.Add(this.button25);
this.Controls.Add(this.button24);
this.Controls.Add(this.button23);
this.Controls.Add(this.button22);
this.Controls.Add(this.button21);
this.Controls.Add(this.button20);
this.Controls.Add(this.button19);
this.Controls.Add(this.button18);
this.Controls.Add(this.button17);
this.Controls.Add(this.button16);
this.Controls.Add(this.button15);
this.Controls.Add(this.button14);
this.Controls.Add(this.button13);
this.Controls.Add(this.button12);
this.Controls.Add(this.button11);
this.Controls.Add(this.button10);
this.Controls.Add(this.button9);
this.Controls.Add(this.button8);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.lblmonth);
this.Controls.Add(this.button1);
this.Controls.Add(this.btnnext);
this.Controls.Add(this.btnprev);
this.Name = "Calendar";
this.Text = "Calendar";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private int DaysInMonth (int Year,int Month)
{
if(Month==2)
{
if(IsLeapYear(Year))
{
return 29;
}
return 28;
}
if(Month==4||Month==6||Month==9||Month==11)
{
return 30;
}
return 31;
}//DateTime.DaysInMonth(
private bool IsLeapYear(int Year)
{
if(Year%4==0&&Year%100!=0||Year%400==0)
return true;
else
return false;
}
private int DayOfWeek(DateTime DateTime)
{
int i;
switch(DateTime.DayOfWeek)
{
case System.DayOfWeek.Monday:
i=1;
break;
case System.DayOfWeek.Tuesday:
i=2;
break;
case System.DayOfWeek.Wednesday:
i=3;
break;
case System.DayOfWeek.Thursday:
i=4;
break;
case System.DayOfWeek.Friday:
i=5;
break;
case System.DayOfWeek.Saturday:
i=6;
break;
case System.DayOfWeek.Sunday:
i=0;
break;
default:
i=-1;
break;
}
return i;
}
private void InitLbl(int Year,int Month)
{
this.lblmonth.Text=Year.ToString()+"年"+Month.ToString()+"月";
this.lblmonth.Refresh();
}
private void InitBtn(int Year,int Month)
{
System.DateTime firstday=Convert.ToDateTime(Year.ToString()+"/"+Month.ToString()+"/"+Convert.ToString(1));
//中間
int iweek=this.DayOfWeek(firstday);
int currMonthDay=DaysInMonth(Year,Month);
int j=0;//按鈕上顯示的文字
for(int i=iweek;i<iweek+currMonthDay;i++)
{
j++;
myButton[i].Text=j.ToString();
myButton[i].Click += new System.EventHandler(this.button_Click);
myButton[i].BackColor=Color.LightSeaGreen;
}
//前面
int prevMonthDay=DaysInMonth(Year,Month-1);
int m=prevMonthDay;//按鈕上顯示的文字
for(int i=iweek-1;i>=0;i--)
{
myButton[i].Text=m.ToString();
myButton[i].Click += new System.EventHandler(this.btnprev_Click);
myButton[i].BackColor=Color.Silver;
m--;
}
//後面
int k=0;//按鈕上顯示的文字
for(int i=iweek+currMonthDay;i<42;i++)
{
k++;
myButton[i].Text=k.ToString();
myButton[i].Click += new System.EventHandler(this.btnnext_Click);
myButton[i].BackColor=Color.Silver;
}
//
//day=System.DateTime.Now.Day;
//myButton[iweek-1+day].Select();
}
private void button_Click(object sender, System.EventArgs e)
{
Button box = (Button)sender;
MessageBox.Show("you selected"+this.year.ToString()+"/"+this.month.ToString()+"/"+box.Text.ToString(),"date" ,MessageBoxButtons.OK);
}
private void btnprev_Click(object sender, System.EventArgs e)
{
unInitBtn();
this.month--;
if(month==0)
{
this.year--;
this.month=12;
}
InitLbl(this.year,this.month);
InitBtn(this.year,this.month);;
}
private void btnnext_Click(object sender, System.EventArgs e)
{
unInitBtn();
this.month++;
if(this.month==13)
{
this.year++;
this.month=1;
}
InitLbl(this.year,this.month);
InitBtn(this.year,this.month);;
}
private void Form1_Load(object sender, System.EventArgs e)
{
System.DateTime currtime=System.DateTime.Now;
this.year=currtime.Year;
this.month=currtime.Month;
this.day=currtime.Day;
InitLbl(this.year,this.month);
InitBtn(this.year,this.month);
this.today.Text="今天是:"+currtime.ToShortDateString();
}
private void unInitBtn()
{
System.DateTime firstday=Convert.ToDateTime(this.year.ToString()+"/"+this.month.ToString()+"/"+Convert.ToString(1));
//中間
int iweek=this.DayOfWeek(firstday);
int currMonthDay=DaysInMonth(this.year,this.month);
int j=0;
for(int i=iweek;i<iweek+currMonthDay;i++)
{
j++;
myButton[i].Text=j.ToString();
myButton[i].Click -= new System.EventHandler(this.button_Click);
myButton[i].BackColor=Color.Coral;
}
//前面
int prevmonthday=DaysInMonth(this.year,this.month-1);
int m=prevmonthday;
for(int i=iweek-1;i>=0;i--)
{
myButton[i].Text=m.ToString();
myButton[i].Click -= new System.EventHandler(this.btnprev_Click);
myButton[i].BackColor=Color.Silver;
m--;
}
//後面
int k=0;
for(int i=iweek+currMonthDay;i<42;i++)
{
k++;
myButton[i].Text=k.ToString();
myButton[i].Click -= new System.EventHandler(this.btnnext_Click);
myButton[i].BackColor=Color.Silver;
}
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CustTool
{
/// <summary>
/// Form1 的摘要描述。
/// </summary>
public class Calendar : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnprev;
private System.Windows.Forms.Button btnnext;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label lblmonth;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.Button button12;
private System.Windows.Forms.Button button13;
private System.Windows.Forms.Button button14;
private System.Windows.Forms.Button button15;
private System.Windows.Forms.Button button16;
private System.Windows.Forms.Button button17;
private System.Windows.Forms.Button button18;
private System.Windows.Forms.Button button19;
private System.Windows.Forms.Button button20;
private System.Windows.Forms.Button button21;
private System.Windows.Forms.Button button22;
private System.Windows.Forms.Button button23;
private System.Windows.Forms.Button button24;
private System.Windows.Forms.Button button25;
private System.Windows.Forms.Button button26;
private System.Windows.Forms.Button button27;
private System.Windows.Forms.Button button28;
private System.Windows.Forms.Button button29;
private System.Windows.Forms.Button button30;
private System.Windows.Forms.Button button31;
private System.Windows.Forms.Button button32;
private System.Windows.Forms.Button button33;
private System.Windows.Forms.Button button34;
private System.Windows.Forms.Button button35;
private System.Windows.Forms.Button button36;
private System.Windows.Forms.Button button37;
private System.Windows.Forms.Button button38;
private System.Windows.Forms.Button button39;
private System.Windows.Forms.Button button40;
private System.Windows.Forms.Button button41;
private System.Windows.Forms.Button button42;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
/// <summary>
/// 設計工具所需的變數。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label today;
private int year;
private int month;
private int day;
private Button[] myButton = new Button[42];
public Calendar()
{
//
// Windows Form 設計工具支援的必要項
//
InitializeComponent();
this.btnprev.Click += new System.EventHandler(this.btnprev_Click);
this.btnnext.Click += new System.EventHandler(this.btnnext_Click);
myButton[0] = button1;
myButton[1] = button2;
myButton[2] = button3;
myButton[3] = button4;
myButton[4] = button5;
myButton[5] = button6;
myButton[6] = button7;
myButton[7] = button8;
myButton[8] = button9;
myButton[9] = button10;
myButton[10] = button11;
myButton[11] = button12;
myButton[12] = button13;
myButton[13] = button14;
myButton[14] = button15;
myButton[15] = button16;
myButton[16] = button17;
myButton[17] = button18;
myButton[18] = button19;
myButton[19] = button20;
myButton[20] = button21;
myButton[21] = button22;
myButton[22] = button23;
myButton[23] = button24;
myButton[24] = button25;
myButton[25] = button26;
myButton[26] = button27;
myButton[27] = button28;
myButton[28] = button29;
myButton[29] = button30;
myButton[30] = button31;
myButton[31] = button32;
myButton[32] = button33;
myButton[33] = button34;
myButton[34] = button35;
myButton[35] = button36;
myButton[36] = button37;
myButton[37] = button38;
myButton[38] = button39;
myButton[39] = button40;
myButton[40] = button41;
myButton[41] = button42;
//
// TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
//
}
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form 設計工具產生的程式碼
/// <summary>
/// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
/// 這個方法的內容。
/// </summary>
private void InitializeComponent()
{
this.btnprev = new System.Windows.Forms.Button();
this.btnnext = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.lblmonth = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.button16 = new System.Windows.Forms.Button();
this.button17 = new System.Windows.Forms.Button();
this.button18 = new System.Windows.Forms.Button();
this.button19 = new System.Windows.Forms.Button();
this.button20 = new System.Windows.Forms.Button();
this.button21 = new System.Windows.Forms.Button();
this.button22 = new System.Windows.Forms.Button();
this.button23 = new System.Windows.Forms.Button();
this.button24 = new System.Windows.Forms.Button();
this.button25 = new System.Windows.Forms.Button();
this.button26 = new System.Windows.Forms.Button();
this.button27 = new System.Windows.Forms.Button();
this.button28 = new System.Windows.Forms.Button();
this.button29 = new System.Windows.Forms.Button();
this.button30 = new System.Windows.Forms.Button();
this.button31 = new System.Windows.Forms.Button();
this.button32 = new System.Windows.Forms.Button();
this.button33 = new System.Windows.Forms.Button();
this.button34 = new System.Windows.Forms.Button();
this.button35 = new System.Windows.Forms.Button();
this.button36 = new System.Windows.Forms.Button();
this.button37 = new System.Windows.Forms.Button();
this.button38 = new System.Windows.Forms.Button();
this.button39 = new System.Windows.Forms.Button();
this.button40 = new System.Windows.Forms.Button();
this.button41 = new System.Windows.Forms.Button();
this.button42 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.today = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnprev
//
this.btnprev.Location = new System.Drawing.Point(16, 48);
this.btnprev.Name = "btnprev";
this.btnprev.Size = new System.Drawing.Size(32, 23);
this.btnprev.TabIndex = 3;
this.btnprev.Text = "<<";
//
// btnnext
//
this.btnnext.Location = new System.Drawing.Point(488, 48);
this.btnnext.Name = "btnnext";
this.btnnext.Size = new System.Drawing.Size(32, 23);
this.btnnext.TabIndex = 9;
this.btnnext.Text = ">>";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 120);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 40);
this.button1.TabIndex = 10;
this.button1.Text = "button1";
//
// lblmonth
//
this.lblmonth.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.lblmonth.Location = new System.Drawing.Point(48, 48);
this.lblmonth.Name = "lblmonth";
this.lblmonth.Size = new System.Drawing.Size(440, 23);
this.lblmonth.TabIndex = 11;
this.lblmonth.Text = "月份";
this.lblmonth.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// button2
//
this.button2.Location = new System.Drawing.Point(83, 120);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 40);
this.button2.TabIndex = 12;
this.button2.Text = "button2";
//
// button3
//
this.button3.Location = new System.Drawing.Point(158, 120);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 40);
this.button3.TabIndex = 13;
this.button3.Text = "button3";
//
// button4
//
this.button4.Location = new System.Drawing.Point(233, 120);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 40);
this.button4.TabIndex = 14;
this.button4.Text = "button4";
//
// button5
//
this.button5.Location = new System.Drawing.Point(308, 120);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 40);
this.button5.TabIndex = 15;
this.button5.Text = "button5";
//
// button6
//
this.button6.Location = new System.Drawing.Point(383, 120);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 40);
this.button6.TabIndex = 16;
this.button6.Text = "button6";
//
// button7
//
this.button7.BackColor = System.Drawing.SystemColors.Control;
this.button7.Location = new System.Drawing.Point(458, 120);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(75, 40);
this.button7.TabIndex = 17;
this.button7.Text = "button7";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 88);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 23);
this.label1.TabIndex = 18;
this.label1.Text = "星期日";
//
// label2
//
this.label2.Location = new System.Drawing.Point(92, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 23);
this.label2.TabIndex = 19;
this.label2.Text = "星期一";
//
// button8
//
this.button8.Location = new System.Drawing.Point(8, 160);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(75, 40);
this.button8.TabIndex = 20;
this.button8.Text = "button8";
//
// button9
//
this.button9.Location = new System.Drawing.Point(83, 160);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(75, 40);
this.button9.TabIndex = 21;
this.button9.Text = "button9";
//
// button10
//
this.button10.Location = new System.Drawing.Point(158, 160);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(75, 40);
this.button10.TabIndex = 22;
this.button10.Text = "button10";
//
// button11
//
this.button11.Location = new System.Drawing.Point(233, 160);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(75, 40);
this.button11.TabIndex = 23;
this.button11.Text = "button11";
//
// button12
//
this.button12.Location = new System.Drawing.Point(308, 160);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(75, 40);
this.button12.TabIndex = 24;
this.button12.Text = "button12";
//
// button13
//
this.button13.Location = new System.Drawing.Point(383, 160);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(75, 40);
this.button13.TabIndex = 25;
this.button13.Text = "button13";
//
// button14
//
this.button14.BackColor = System.Drawing.SystemColors.Control;
this.button14.Location = new System.Drawing.Point(458, 160);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(75, 40);
this.button14.TabIndex = 26;
this.button14.Text = "button14";
//
// button15
//
this.button15.Location = new System.Drawing.Point(8, 200);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(75, 40);
this.button15.TabIndex = 27;
this.button15.Text = "button15";
//
// button16
//
this.button16.Location = new System.Drawing.Point(83, 200);
this.button16.Name = "button16";
this.button16.Size = new System.Drawing.Size(75, 40);
this.button16.TabIndex = 28;
this.button16.Text = "button16";
//
// button17
//
this.button17.Location = new System.Drawing.Point(158, 200);
this.button17.Name = "button17";
this.button17.Size = new System.Drawing.Size(75, 40);
this.button17.TabIndex = 29;
this.button17.Text = "button17";
//
// button18
//
this.button18.Location = new System.Drawing.Point(233, 200);
this.button18.Name = "button18";
this.button18.Size = new System.Drawing.Size(75, 40);
this.button18.TabIndex = 30;
this.button18.Text = "button18";
//
// button19
//
this.button19.Location = new System.Drawing.Point(308, 200);
this.button19.Name = "button19";
this.button19.Size = new System.Drawing.Size(75, 40);
this.button19.TabIndex = 31;
this.button19.Text = "button19";
//
// button20
//
this.button20.Location = new System.Drawing.Point(383, 200);
this.button20.Name = "button20";
this.button20.Size = new System.Drawing.Size(75, 40);
this.button20.TabIndex = 32;
this.button20.Text = "button20";
//
// button21
//
this.button21.Location = new System.Drawing.Point(458, 200);
this.button21.Name = "button21";
this.button21.Size = new System.Drawing.Size(75, 40);
this.button21.TabIndex = 33;
this.button21.Text = "button21";
//
// button22
//
this.button22.Location = new System.Drawing.Point(8, 240);
this.button22.Name = "button22";
this.button22.Size = new System.Drawing.Size(75, 40);
this.button22.TabIndex = 34;
this.button22.Text = "button22";
//
// button23
//
this.button23.Location = new System.Drawing.Point(83, 240);
this.button23.Name = "button23";
this.button23.Size = new System.Drawing.Size(75, 40);
this.button23.TabIndex = 35;
this.button23.Text = "button23";
//
// button24
//
this.button24.Location = new System.Drawing.Point(158, 240);
this.button24.Name = "button24";
this.button24.Size = new System.Drawing.Size(75, 40);
this.button24.TabIndex = 36;
this.button24.Text = "button24";
//
// button25
//
this.button25.Location = new System.Drawing.Point(233, 240);
this.button25.Name = "button25";
this.button25.Size = new System.Drawing.Size(75, 40);
this.button25.TabIndex = 37;
this.button25.Text = "button25";
//
// button26
//
this.button26.Location = new System.Drawing.Point(308, 240);
this.button26.Name = "button26";
this.button26.Size = new System.Drawing.Size(75, 40);
this.button26.TabIndex = 38;
this.button26.Text = "button26";
//
// button27
//
this.button27.Location = new System.Drawing.Point(383, 240);
this.button27.Name = "button27";
this.button27.Size = new System.Drawing.Size(75, 40);
this.button27.TabIndex = 39;
this.button27.Text = "button27";
//
// button28
//
this.button28.Location = new System.Drawing.Point(458, 240);
this.button28.Name = "button28";
this.button28.Size = new System.Drawing.Size(75, 40);
this.button28.TabIndex = 40;
this.button28.Text = "button28";
//
// button29
//
this.button29.Location = new System.Drawing.Point(8, 280);
this.button29.Name = "button29";
this.button29.Size = new System.Drawing.Size(75, 40);
this.button29.TabIndex = 41;
this.button29.Text = "button29";
//
// button30
//
this.button30.Location = new System.Drawing.Point(83, 280);
this.button30.Name = "button30";
this.button30.Size = new System.Drawing.Size(75, 40);
this.button30.TabIndex = 42;
this.button30.Text = "button30";
//
// button31
//
this.button31.Location = new System.Drawing.Point(158, 280);
this.button31.Name = "button31";
this.button31.Size = new System.Drawing.Size(75, 40);
this.button31.TabIndex = 43;
this.button31.Text = "button31";
//
// button32
//
this.button32.Location = new System.Drawing.Point(233, 280);
this.button32.Name = "button32";
this.button32.Size = new System.Drawing.Size(75, 40);
this.button32.TabIndex = 44;
this.button32.Text = "button32";
//
// button33
//
this.button33.Location = new System.Drawing.Point(308, 280);
this.button33.Name = "button33";
this.button33.Size = new System.Drawing.Size(75, 40);
this.button33.TabIndex = 45;
this.button33.Text = "button33";
//
// button34
//
this.button34.Location = new System.Drawing.Point(383, 280);
this.button34.Name = "button34";
this.button34.Size = new System.Drawing.Size(75, 40);
this.button34.TabIndex = 46;
this.button34.Text = "button34";
//
// button35
//
this.button35.Location = new System.Drawing.Point(458, 280);
this.button35.Name = "button35";
this.button35.Size = new System.Drawing.Size(75, 40);
this.button35.TabIndex = 47;
this.button35.Text = "button35";
//
// button36
//
this.button36.Location = new System.Drawing.Point(8, 320);
this.button36.Name = "button36";
this.button36.Size = new System.Drawing.Size(75, 40);
this.button36.TabIndex = 48;
this.button36.Text = "button36";
//
// button37
//
this.button37.Location = new System.Drawing.Point(83, 320);
this.button37.Name = "button37";
this.button37.Size = new System.Drawing.Size(75, 40);
this.button37.TabIndex = 49;
this.button37.Text = "button37";
//
// button38
//
this.button38.Location = new System.Drawing.Point(158, 320);
this.button38.Name = "button38";
this.button38.Size = new System.Drawing.Size(75, 40);
this.button38.TabIndex = 50;
this.button38.Text = "button38";
//
// button39
//
this.button39.Location = new System.Drawing.Point(233, 320);
this.button39.Name = "button39";
this.button39.Size = new System.Drawing.Size(75, 40);
this.button39.TabIndex = 51;
this.button39.Text = "button39";
//
// button40
//
this.button40.Location = new System.Drawing.Point(308, 320);
this.button40.Name = "button40";
this.button40.Size = new System.Drawing.Size(75, 40);
this.button40.TabIndex = 52;
this.button40.Text = "button40";
//
// button41
//
this.button41.Location = new System.Drawing.Point(383, 320);
this.button41.Name = "button41";
this.button41.Size = new System.Drawing.Size(75, 40);
this.button41.TabIndex = 53;
this.button41.Text = "button41";
//
// button42
//
this.button42.Location = new System.Drawing.Point(458, 320);
this.button42.Name = "button42";
this.button42.Size = new System.Drawing.Size(75, 40);
this.button42.TabIndex = 54;
this.button42.Text = "button42";
//
// label3
//
this.label3.Location = new System.Drawing.Point(168, 88);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(48, 23);
this.label3.TabIndex = 55;
this.label3.Text = "星期二";
//
// label4
//
this.label4.Location = new System.Drawing.Point(244, 88);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(48, 23);
this.label4.TabIndex = 56;
this.label4.Text = "星期三";
//
// label5
//
this.label5.Location = new System.Drawing.Point(320, 88);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(48, 23);
this.label5.TabIndex = 57;
this.label5.Text = "星期四";
//
// label6
//
this.label6.Location = new System.Drawing.Point(396, 88);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(48, 23);
this.label6.TabIndex = 58;
this.label6.Text = "星期五";
//
// label7
//
this.label7.Location = new System.Drawing.Point(472, 88);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(48, 23);
this.label7.TabIndex = 59;
this.label7.Text = "星期六";
//
// today
//
this.today.Location = new System.Drawing.Point(48, 376);
this.today.Name = "today";
this.today.Size = new System.Drawing.Size(280, 23);
this.today.TabIndex = 60;
this.today.Text = "today";
//
// Calendar
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(552, 405);
this.Controls.Add(this.today);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.button42);
this.Controls.Add(this.button41);
this.Controls.Add(this.button40);
this.Controls.Add(this.button39);
this.Controls.Add(this.button38);
this.Controls.Add(this.button37);
this.Controls.Add(this.button36);
this.Controls.Add(this.button35);
this.Controls.Add(this.button34);
this.Controls.Add(this.button33);
this.Controls.Add(this.button32);
this.Controls.Add(this.button31);
this.Controls.Add(this.button30);
this.Controls.Add(this.button29);
this.Controls.Add(this.button28);
this.Controls.Add(this.button27);
this.Controls.Add(this.button26);
this.Controls.Add(this.button25);
this.Controls.Add(this.button24);
this.Controls.Add(this.button23);
this.Controls.Add(this.button22);
this.Controls.Add(this.button21);
this.Controls.Add(this.button20);
this.Controls.Add(this.button19);
this.Controls.Add(this.button18);
this.Controls.Add(this.button17);
this.Controls.Add(this.button16);
this.Controls.Add(this.button15);
this.Controls.Add(this.button14);
this.Controls.Add(this.button13);
this.Controls.Add(this.button12);
this.Controls.Add(this.button11);
this.Controls.Add(this.button10);
this.Controls.Add(this.button9);
this.Controls.Add(this.button8);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.lblmonth);
this.Controls.Add(this.button1);
this.Controls.Add(this.btnnext);
this.Controls.Add(this.btnprev);
this.Name = "Calendar";
this.Text = "Calendar";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private int DaysInMonth (int Year,int Month)
{
if(Month==2)
{
if(IsLeapYear(Year))
{
return 29;
}
return 28;
}
if(Month==4||Month==6||Month==9||Month==11)
{
return 30;
}
return 31;
}//DateTime.DaysInMonth(
private bool IsLeapYear(int Year)
{
if(Year%4==0&&Year%100!=0||Year%400==0)
return true;
else
return false;
}
private int DayOfWeek(DateTime DateTime)
{
int i;
switch(DateTime.DayOfWeek)
{
case System.DayOfWeek.Monday:
i=1;
break;
case System.DayOfWeek.Tuesday:
i=2;
break;
case System.DayOfWeek.Wednesday:
i=3;
break;
case System.DayOfWeek.Thursday:
i=4;
break;
case System.DayOfWeek.Friday:
i=5;
break;
case System.DayOfWeek.Saturday:
i=6;
break;
case System.DayOfWeek.Sunday:
i=0;
break;
default:
i=-1;
break;
}
return i;
}
private void InitLbl(int Year,int Month)
{
this.lblmonth.Text=Year.ToString()+"年"+Month.ToString()+"月";
this.lblmonth.Refresh();
}
private void InitBtn(int Year,int Month)
{
System.DateTime firstday=Convert.ToDateTime(Year.ToString()+"/"+Month.ToString()+"/"+Convert.ToString(1));
//中間
int iweek=this.DayOfWeek(firstday);
int currMonthDay=DaysInMonth(Year,Month);
int j=0;//按鈕上顯示的文字
for(int i=iweek;i<iweek+currMonthDay;i++)
{
j++;
myButton[i].Text=j.ToString();
myButton[i].Click += new System.EventHandler(this.button_Click);
myButton[i].BackColor=Color.LightSeaGreen;
}
//前面
int prevMonthDay=DaysInMonth(Year,Month-1);
int m=prevMonthDay;//按鈕上顯示的文字
for(int i=iweek-1;i>=0;i--)
{
myButton[i].Text=m.ToString();
myButton[i].Click += new System.EventHandler(this.btnprev_Click);
myButton[i].BackColor=Color.Silver;
m--;
}
//後面
int k=0;//按鈕上顯示的文字
for(int i=iweek+currMonthDay;i<42;i++)
{
k++;
myButton[i].Text=k.ToString();
myButton[i].Click += new System.EventHandler(this.btnnext_Click);
myButton[i].BackColor=Color.Silver;
}
//
//day=System.DateTime.Now.Day;
//myButton[iweek-1+day].Select();
}
private void button_Click(object sender, System.EventArgs e)
{
Button box = (Button)sender;
MessageBox.Show("you selected"+this.year.ToString()+"/"+this.month.ToString()+"/"+box.Text.ToString(),"date" ,MessageBoxButtons.OK);
}
private void btnprev_Click(object sender, System.EventArgs e)
{
unInitBtn();
this.month--;
if(month==0)
{
this.year--;
this.month=12;
}
InitLbl(this.year,this.month);
InitBtn(this.year,this.month);;
}
private void btnnext_Click(object sender, System.EventArgs e)
{
unInitBtn();
this.month++;
if(this.month==13)
{
this.year++;
this.month=1;
}
InitLbl(this.year,this.month);
InitBtn(this.year,this.month);;
}
private void Form1_Load(object sender, System.EventArgs e)
{
System.DateTime currtime=System.DateTime.Now;
this.year=currtime.Year;
this.month=currtime.Month;
this.day=currtime.Day;
InitLbl(this.year,this.month);
InitBtn(this.year,this.month);
this.today.Text="今天是:"+currtime.ToShortDateString();
}
private void unInitBtn()
{
System.DateTime firstday=Convert.ToDateTime(this.year.ToString()+"/"+this.month.ToString()+"/"+Convert.ToString(1));
//中間
int iweek=this.DayOfWeek(firstday);
int currMonthDay=DaysInMonth(this.year,this.month);
int j=0;
for(int i=iweek;i<iweek+currMonthDay;i++)
{
j++;
myButton[i].Text=j.ToString();
myButton[i].Click -= new System.EventHandler(this.button_Click);
myButton[i].BackColor=Color.Coral;
}
//前面
int prevmonthday=DaysInMonth(this.year,this.month-1);
int m=prevmonthday;
for(int i=iweek-1;i>=0;i--)
{
myButton[i].Text=m.ToString();
myButton[i].Click -= new System.EventHandler(this.btnprev_Click);
myButton[i].BackColor=Color.Silver;
m--;
}
//後面
int k=0;
for(int i=iweek+currMonthDay;i<42;i++)
{
k++;
myButton[i].Text=k.ToString();
myButton[i].Click -= new System.EventHandler(this.btnnext_Click);
myButton[i].BackColor=Color.Silver;
}
}
}
}