DateChooser源码--DataChooserDesigner.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using System.ComponentModel.Design;
using System.ComponentModel;
namespace CNBlogs.DCT.THIN.Design
{
/// <summary>
/// DataChooserDesigner 的摘要说明。
/// </summary>
public class DataChooserDesigner:ControlDesigner
{
public DataChooserDesigner()
{
this.bFirstTime = true;
this.IsDroppedDown = false;
this.showHideDropdown = new DesignerVerb(("显示/隐藏下拉日期框"), new EventHandler(this.OnShowHideDropDownClick));
this.about = new DesignerVerb("关于",new EventHandler(this.OnAbloutClick));
this.verbs = new DesignerVerbCollection();
}
public override string GetDesignTimeHtml()
{
string text1 = null;
if ((base.Component == null) || !(base.Component is DateChooser))
{
return base.GetDesignTimeHtml();
}
try
{
DateChooser chooser1 = (DateChooser) base.Component;
ControlCollection cc = chooser1.Controls;
text1 = base.GetDesignTimeHtml();
if(chooser1.EnableClientScript)
{
this.showHideDropdown.Visible = true;
if(this.IsDroppedDown)
{
string lightColor = chooser1.CCS(chooser1.LightColor);
string midColor = chooser1.CCS(chooser1.MidColor);
string darkColor = chooser1.CCS(chooser1.DarkColor);
string borderColor = chooser1.CCS(chooser1.BorderColor);
string currentBack = chooser1.CCS(chooser1.CurrentBackColor);
string currentFore = chooser1.CCS(chooser1.CurrentForeColor);
string todayBack = chooser1.CCS(chooser1.TodayBackColor);
string todayFore = chooser1.CCS(chooser1.TodayForeColor);
string otherBack = chooser1.CCS(chooser1.OtherBackColor);
string otherFore = chooser1.CCS(chooser1.OtherForeColor);
string monthBack = chooser1.CCS(chooser1.MonthBackColor);
string monthFore = chooser1.CCS(chooser1.MonthForeColor);
bool showbtn = chooser1.ShowButton;
DateTime selected;
selected = (chooser1.SelectedDate=="")?DateTime.Today:DateTime.Parse(chooser1.SelectedDate);
if(selected == DateTime.MinValue)
selected = DateTime.Today;
bool cultureCn = chooser1.Culture == Culture.Cn;
bool allowNull = chooser1.AllowNull;
text1 = "<table cellpadding=0 cellspacing=0 border=0><tr><td>"+text1+"</td></tr><tr><td>";
text1 += GetDropString(lightColor,midColor,darkColor,borderColor,currentBack,currentFore,
todayBack,todayFore,otherBack,otherFore,monthFore,monthBack,cultureCn,selected,allowNull)+"</td></tr></table>";
return text1;
}
}
else
{
this.showHideDropdown.Visible = false;
}
}
catch (Exception exception1)
{
text1 = this.GetErrorDesignTimeHtml(exception1);
}
return text1;
}
private string GetDropString(string lightColor, string midColor,string darkColor,string borderColor,string currentBack,string currentFore
,string todayBack,string todayFore,string otherBack,string otherFore,string monthFore,string monthBack
,bool cultureCn,DateTime selected,bool allowNull)
{
string v = string.Format(@"
<TABLE style=""BORDER-RIGHT:{3} 1px solid; BORDER-TOP: {3} 1px solid; DISPLAY: block; FONT-SIZE: 9pt; BORDER-LEFT: {3} 1px solid; CURSOR: default; BORDER-BOTTOM: {3} 1px solid; HEIGHT: 200px; BACKGROUND-COLOR: {0}"" border=0><TBODY>
<TR height=16>
<TD width=""100%"">
<TABLE cellSpacing=0 cellPadding=0 width=""100%"" border=0>
<TBODY>
<TR>
<TD style=""FONT-SIZE: 9pt; CURSOR: hand; COLOR: {2}; FONT-FAMILY: webdings"" width=""10%"">9</TD>
<TD style=""FONT-SIZE: 9pt; CURSOR: hand; COLOR: {2}; FONT-FAMILY: webdings"" width=""10%"">7</TD>
<TD width=""30%""><SELECT><OPTION>2000</OPTION></SELECT></TD>
<TD width=""30%""><SELECT><OPTION>Jan</OPTION></SELECT></TD>
<TD style=""FONT-SIZE: 9pt; CURSOR: hand; COLOR: {2}; FONT-FAMILY: webdings"" width=""10%"">8</TD>
<TD style=""FONT-SIZE: 9pt; CURSOR: hand; COLOR: {2}; FONT-FAMILY: webdings"" width=""10%"">:</TD></TR></TBODY></TABLE></TD></TR>
<TR height=2>
<TD align=middle colSpan=3>
<DIV id=DataChooser_separator style=""FONT-SIZE: 0px; BORDER-BOTTOM: {2} 1px solid""> </DIV>
<DIV style=""BORDER-TOP: #eeeeee 1px solid; FONT-SIZE: 0px""> </DIV></TD></TR>
<TR>
<TD colSpan=3>
<TABLE id=DataChooser_tabledays style=""BORDER-RIGHT: {3} 1px solid; BORDER-TOP: {3} 1px solid; BORDER-LEFT: {3} 1px solid; BORDER-BOTTOM: {3} 1px solid; BORDER-COLLAPSE: collapse"" borderColor={3} height=124 width=202 border=1>
<TBODY>
<TR style=""FONT-WEIGHT: bold; COLOR: {2}; FONT-FAMILY: fixedsys; BACKGROUND-COLOR: {1}"">
<TD align=middle>sun</TD>
<TD align=middle>mon</TD>
<TD align=middle>tue</TD>
<TD align=middle>wed</TD>
<TD align=middle>thu</TD>
<TD align=middle>fri</TD>
<TD align=middle>sat</TD></TR>
CreateDays</TBODY></TABLE></TD></TR>
<TR>
<TD align=middle><BUTTON id=DataChooser_btntd hideFocus style=""BORDER-RIGHT: {3} 1px outset; BORDER-TOP: {3} 1px outset; FONT-SIZE: 9pt; BORDER-LEFT: {3} 1px outset; WIDTH: 62px; CURSOR: hand; PADDING-TOP: 2px; BORDER-BOTTOM: {3} 1px outset; BACKGROUND-COLOR: {2}"" onfocus=blur()>Today</BUTTON> <BUTTON id=DataChooser_btnbl hideFocus style=""BORDER-RIGHT: {3} 1px outset; BORDER-TOP: {3} 1px outset; FONT-SIZE: 9pt; BORDER-LEFT: {3} 1px outset; WIDTH: 62px; CURSOR: hand; PADDING-TOP: 2px; BORDER-BOTTOM: {3} 1px outset; BACKGROUND-COLOR: {2}"" onfocus=blur()>Clear</BUTTON> <BUTTON id=DataChooser_btnok hideFocus style=""BORDER-RIGHT: {3} 1px outset; BORDER-TOP: {3} 1px outset; FONT-SIZE: 9pt; BORDER-LEFT: {3} 1px outset; WIDTH: 62px; CURSOR: hand; PADDING-TOP: 2px; BORDER-BOTTOM: {3} 1px outset; BACKGROUND-COLOR: {2}"" onfocus=blur()>Close</BUTTON> </TD></TR></TBODY></TABLE>",lightColor, midColor,darkColor, borderColor, currentBack,todayBack,otherFore,monthFore,cultureCn);
if(!allowNull)
v = v.Replace("<BUTTON id=DataChooser_btnbl hideFocus style=\"BORDER-RIGHT:","<BUTTON id=DataChooser_btnbl hideFocus style=\"display:none;BORDER-RIGHT:");
if(cultureCn)
{
v = v.Replace(@"<TD align=middle>sun</TD>
<TD align=middle>mon</TD>
<TD align=middle>tue</TD>
<TD align=middle>wed</TD>
<TD align=middle>thu</TD>
<TD align=middle>fri</TD>
<TD align=middle>sat</TD>",@"<TD align=middle>日</TD>
<TD align=middle>一</TD>
<TD align=middle>二</TD>
<TD align=middle>三</TD>
<TD align=middle>四</TD>
<TD align=middle>五</TD>
<TD align=middle>六</TD>").Replace(">Today<",">今天<").Replace(">Clear<",">清空<").Replace(">Close<",">关闭<");
string month="";
switch(selected.Month)
{
case 1:
month = "一月";break;
case 2:
month = "二月";break;
case 3:
month = "三月";break;
case 4:
month = "四月";break;
case 5:
month = "五月";break;
case 6:
month = "六月";break;
case 7:
month = "七月";break;
case 8:
month = "八月";break;
case 9:
month = "九月";break;
case 10:
month = "十月";break;
case 11:
month = "十一月";break;
case 12:
month = "十二月";break;
}
v = v.Replace("<OPTION>Jan</OPTION>","<OPTION>"+month+"</OPTION>");
}
else
{
string month="";
switch(selected.Month)
{
case 1:
month = "Jan";break;
case 2:
month = "Feb";break;
case 3:
month = "Mar";break;
case 4:
month = "Apr";break;
case 5:
month = "May";break;
case 6:
month = "Jun";break;
case 7:
month = "Jul";break;
case 8:
month = "Aug";break;
case 9:
month = "Sep";break;
case 10:
month = "Oct";break;
case 11:
month = "Nov";break;
case 12:
month = "Dec";break;
}
v = v.Replace("<OPTION>Jan</OPTION>","<OPTION>"+month+"</OPTION>");
}
v = v.Replace("<OPTION>2000</OPTION>","<OPTION>"+selected.Year.ToString()+"</OPTION>");
v = v.Replace("CreateDays",CreateDays(selected,currentBack,currentFore,todayBack,todayFore,otherBack,otherFore,monthBack,monthFore));
return v;
}
protected override string GetErrorDesignTimeHtml(Exception e)
{
string text1 = "DesignTimeErrorMessage" + e.Message;
if (e.InnerException != null)
{
text1 = text1 + "<br>DateChooser 博客园控件开发团队(www.cnblogs.com/team/dotnetcontrols.htm)<br>THIN(www.cnblogs.com/thinhunan)作品<br>在使用上有任何问题和建议请联系我,本团队非常乐意根据使用者的建议制作控件<br><span style='background:yellow'>" + e.InnerException.Message + "</span>";
}
return base.CreatePlaceHolderDesignTimeHtml(text1);
}
public override void Initialize(IComponent component)
{
base.Initialize(component);
}
public void OnShowHideDropDownClick(object o, EventArgs e)
{
this.IsDroppedDown = !this.IsDroppedDown;
base.IsDirty = true;
this.UpdateDesignTimeHtml();
}
public void OnAbloutClick(object o ,EventArgs e)
{
Help help = new Help();
help.ShowDialog();
}
private bool bFirstTime;
private bool IsDroppedDown = false;
public override DesignerVerbCollection Verbs
{
get
{
if (this.bFirstTime && (base.Component != null))
{
this.verbs = base.Verbs;
this.verbs.Add(this.showHideDropdown);
this.verbs.Add(this.about);
this.bFirstTime = false;
}
this.showHideDropdown.Visible = true;
this.about.Visible = true;
return this.verbs;
}
}
private string CreateDays(DateTime selected,
string currentBack,string currentFore,
string todayBack,string todayFore,
string otherBack,string otherFore,
string monthBack,string monthFore)
{
DateTime dtFirst = new DateTime(selected.Year,selected.Month,1);
DateTime dtEnd = new DateTime(selected.Year,selected.Month+1,1);
if(!(dtFirst.Year == 1 && dtFirst.Month == 1))
dtFirst = dtFirst.AddDays(-1*((int)dtFirst.DayOfWeek));
if(dtEnd.DayOfWeek != DayOfWeek.Sunday)
dtEnd = dtEnd.AddDays(7-((int)dtEnd.DayOfWeek));
string v = "";
for(DateTime dt = dtFirst;dt<dtEnd;dt = dt.AddDays(1))
{
string td;
if(dt.DayOfWeek == DayOfWeek.Sunday)
v += "<tr>";
td = "<TD style=\"FONT-SIZE: 9pt; CURSOR: hand; COLOR: "+otherFore+";BACKGROUND-COLOR:"+otherBack+"; FONT-FAMILY: fixedsys;\" align=center width=16 height=16>"+dt.Day.ToString()+"</TD>";
if(dt.Month == selected.Month)
td= "<TD style=\"FONT-SIZE: 9pt; CURSOR: hand; COLOR: "+monthFore+";BACKGROUND-COLOR: "+monthBack+"; FONT-FAMILY: fixedsys;\" align=center width=16 height=16>"+dt.Day.ToString()+"</TD>";
if(dt.Month == DateTime.Today.Month && dt.Day == DateTime.Today.Day)
td= "<TD style=\"FONT-SIZE: 9pt; CURSOR: hand; COLOR: "+todayFore+"; FONT-FAMILY: fixedsys; BACKGROUND-COLOR: "+todayBack+";\" align=center width=16 height=16>"+dt.Day.ToString()+"</TD>";
if(dt.Month == selected.Month && dt.Day == selected.Day)
td= "<TD style=\"FONT-WEIGHT: bold; FONT-SIZE: 9pt; CURSOR: hand; COLOR: "+currentFore+"; FONT-FAMILY: fixedsys; BACKGROUND-COLOR: "+currentBack+";\" align=center width=16 height=16>"+dt.Day.ToString()+"</TD>";
v += td;
if(dt.DayOfWeek == DayOfWeek.Saturday)
v+= "<tr>";
}
return v;
}
private DesignerVerb showHideDropdown;
private DesignerVerb about;
private DesignerVerbCollection verbs;
}
}