跟據DateTimePicker的選擇,在旁邊的Label中自動的顯示星期...
一:先寫一個顯示星期的UIControl,這個UIControl從外外接收到一日期參數,自動變換成日期並顯示於Label.....
using System;
using System.Windows.Forms;
using System.Drawing;
namespace XUIControls
{
/// <summary>
/// Summary description for XWeeks.
/// </summary>
public class XWeeks:System.Windows.Forms.Label
{
public XWeeks()
{
//
// TODO: Add constructor logic here
//
this.AutoSize=false;
this.Height=24;
this.BorderStyle=System.Windows.Forms.BorderStyle.Fixed3D;
this.TextAlign=System.Drawing.ContentAlignment.MiddleLeft;
this.Font=new System.Drawing.Font("Microsoft Sans Serif",9F,System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
}
private string[] Week_Cdays=new string[7]
{
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六",
"星期日",
};
private string GetDays(string astg_days)
{
string lstg_days;
switch(astg_days)
{
case "Monday":
this.ForeColor=System.Drawing.Color.Black;
lstg_days=Week_Cdays[0];
break;
case "Tuesday":
this.ForeColor=System.Drawing.Color.Black;
lstg_days=Week_Cdays[1];
break;
case "Wednesday":
this.ForeColor=System.Drawing.Color.Black;
lstg_days=Week_Cdays[2];
break;
case "Thursday":
this.ForeColor=System.Drawing.Color.Black;
lstg_days=Week_Cdays[3];
break;
case "Friday":
this.ForeColor=System.Drawing.Color.Black;
lstg_days=Week_Cdays[4];
break;
case "Saturday":
this.ForeColor=System.Drawing.Color.Green;
lstg_days=Week_Cdays[5];
break;
case "Sunday":
this.ForeColor=System.Drawing.Color.Red;
lstg_days=Week_Cdays[6];
break;
default:
this.ForeColor=System.Drawing.Color.RoyalBlue;
lstg_days="星期錯!";
break;
}
return lstg_days;
}
public override string Text
{
get
{
string lstg_days;
if(base.Text=="")
{
lstg_days=System.DateTime.Now.DayOfWeek.ToString();
}
else
{
lstg_days=base.Text;
}
return GetDays(lstg_days);
}
set
{
base.Text = value;
}
}
}
}
二:再寫一個UserControl, 將剛才新建的UIControl拖至於UserContrl,並在UserContrl上放至一個DataTimerPicker...,這一部分主要在DataTimerPicker的選擇事件,寫UIControl的操作,向UIControl是傳日期參數...
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace My_Diarys.UserControls
{
/// <summary>
/// Summary description for UserControl_Header.
/// </summary>
public class UserControl_Header : System.Windows.Forms.UserControl
{
private XUIControls.XDataTimePicker dtp_datetime;
private XUIControls.XWeeks lbl_weekdays;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public UserControl_Header()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitializeComponent call
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dtp_datetime = new XUIControls.XDataTimePicker();
this.lbl_weekdays = new XUIControls.XWeeks();
this.SuspendLayout();
//
// dtp_datetime
//
this.dtp_datetime.CalendarFont = new System.Drawing.Font("Microsoft Sans Serif", 7.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.dtp_datetime.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.dtp_datetime.Location = new System.Drawing.Point(2, 2);
this.dtp_datetime.Name = "dtp_datetime";
this.dtp_datetime.Size = new System.Drawing.Size(100, 23);
this.dtp_datetime.TabIndex = 6;
this.dtp_datetime.ValueChanged += new System.EventHandler(this.dtp_datetime_ValueChanged);
//
// lbl_weekdays
//
this.lbl_weekdays.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lbl_weekdays.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lbl_weekdays.ForeColor = System.Drawing.Color.Black;
this.lbl_weekdays.Location = new System.Drawing.Point(104, 2);
this.lbl_weekdays.Name = "lbl_weekdays";
this.lbl_weekdays.Size = new System.Drawing.Size(48, 23);
this.lbl_weekdays.TabIndex = 7;
this.lbl_weekdays.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// UserControl_Header
//
this.Controls.Add(this.dtp_datetime);
this.Controls.Add(this.lbl_weekdays);
this.Name = "UserControl_Header";
this.Size = new System.Drawing.Size(708, 86);
this.ResumeLayout(false);
}
#endregion
private void dtp_datetime_ValueChanged(object sender, System.EventArgs e)
{
this.lbl_weekdays.Text=this.dtp_datetime.Value.DayOfWeek.ToString();
}
}
}
效果圖: