700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > vs中Calendar控件的一些使用

vs中Calendar控件的一些使用

时间:2022-08-31 01:45:30

相关推荐

vs中Calendar控件的一些使用

一.返回指定日期前后的某一日期;

// Sets a DateTime to April 3, 2002 of the Gregorian calendar.

DateTime myDT = new DateTime( 2002, 4, 3, new GregorianCalendar() );

// Uses the default calendar of the InvariantCulture.

Calendar myCal = CultureInfo.InvariantCulture.Calendar;

// Displays the values of the DateTime.

Console.WriteLine( "April 3, 2002 of the Gregorian calendar:" );

DisplayValues( myCal, myDT );

// Adds 5 to every component of the DateTime.

myDT = myCal.AddYears( myDT, 5 );

myDT = myCal.AddMonths( myDT, 5 );

myDT = myCal.AddWeeks( myDT, 5 );

myDT = myCal.AddDays( myDT, 5 );

myDT = myCal.AddHours( myDT, 5 );

myDT = myCal.AddMinutes( myDT, 5 );

myDT = myCal.AddSeconds( myDT, 5 );

myDT = myCal.AddMilliseconds( myDT, 5 );

// Sets a DateTime to April 3, 2002 of the Gregorian calendar.

DateTime myDT = new DateTime( 2002, 4, 3, new GregorianCalendar() );

// Uses the default calendar of the InvariantCulture.

Calendar myCal = CultureInfo.InvariantCulture.Calendar;

// Displays the values of the DateTime.

Console.WriteLine( "April 3, 2002 of the Gregorian calendar:" );

DisplayValues( myCal, myDT );

// Adds 5 to every component of the DateTime.

myDT = myCal.AddYears( myDT, 5 );

myDT = myCal.AddMonths( myDT, 5 );

myDT = myCal.AddWeeks( myDT, 5 );

myDT = myCal.AddDays( myDT, 5 );

myDT = myCal.AddHours( myDT, 5 );

myDT = myCal.AddMinutes( myDT, 5 );

myDT = myCal.AddSeconds( myDT, 5 );

myDT = myCal.AddMilliseconds( myDT, 5 );

二.在 Calendar Web 服务器控件中自定义个别日

默认情况下,Calendar控件中的日只显示为数字。(如果启用日选定,则数字将显示为链接。有关详细信息,请参见在 Calendar Web 服务器控件中控制用户日期选定。)但是,您可以自定义单个日的内容和外观,如下所示:

以编程方式突出显示某些日。例如,以不同的颜色显示假日。以编程方式指定是否可以选定个别日。向日显示中添加信息,例如约会或事件信息。

Calendar控件正在创建要发送到浏览器的输出时,它将引发一个您可以处理的DayRender事件。控件在准备要显示的日时将为每个日调用您的方法,然后您可采用编程的方式检查正显示的是哪个日期,并对其进行适当的自定义。

DayRender事件的方法带有两个参数,包括引发事件的控件(Calendar控件)的引用和一个DayRenderEvent类型的对象。DayRenderEvent对象提供对另外两个对象的访问:

Cell,它是一个TableCell对象,可用于设置个别日的外观。Day,可用于查询关于呈现日的信息,控制是否可选定该日,以及将内容添加到日中。Day对象支持各种可用于了解有关日的信息的属性(例如,IsSelectedIsToday等)。它还支持Controls集合,可操作该集合以将内容添加到日中。

自定义个别日的外观

Calendar控件的DayRender事件创建一个方法。该事件应该具有以下签名:

' Visual BasicPrivate Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender' Additional code hereEnd Sub// C#private void Calendar1_DayRender (object sender, System.Web.UI.WebControls.DayRenderEventArgs e){// Additional code here}

在该方法中,设置通过DayRenderEvent参数可用的Cell对象的属性,如下例所示:

' Visual BasicIf (e.Day.IsToday) Thene.Cell.BackColor = System.Drawing.Color.RedEnd If// C#if (e.Day.IsToday){e.Cell.BackColor = System.Drawing.Color.Red;}

下例显示一个简单但完整的方法,该方法阐释了如何更改个别日的外观。该方法使日历中的节假日呈现为黄色,而周末呈现为绿色。

' Visual BasicPublic Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender' Display vacation dates in yellow boxes with purple borders.Dim vacationStyle As New Style()With vacationStyle.BackColor = System.Drawing.Color.Yellow.BorderColor = System.Drawing.Color.Purple.BorderWidth = New Unit(3)End With' Display weekend dates in green boxes.Dim weekendStyle As New Style()weekendStyle.BackColor = System.Drawing.Color.Green' Vacation is from Nov 23, 2000 to Nov 30, 2000.If ((e.Day.Date >= New Date(2000, 11, 23)) _And (e.Day.Date <= New Date(2000, 11, 30))) Thene.Cell.ApplyStyle(vacationStyle)ElseIf (e.Day.IsWeekend) Thene.Cell.ApplyStyle(weekendStyle)End IfEnd Sub// C#private void Calendar1_DayRender (object sender, System.Web.UI.WebControls.DayRenderEventArgs e){// Display vacation dates in yellow boxes with purple borders.Style vacationStyle = new Style();vacationStyle.BackColor = System.Drawing.Color.Yellow;vacationStyle.BorderColor = System.Drawing.Color.Purple;vacationStyle.BorderWidth = 3;// Display weekend dates in green boxes.Style weekendStyle = new Style();weekendStyle.BackColor = System.Drawing.Color.Green;if ((e.Day.Date >= new DateTime(2000,11,23)) &&(e.Day.Date <= new DateTime(2000,11,30))){// Apply the vacation style to the vacation dates.e.Cell.ApplyStyle(vacationStyle);}else if (e.Day.IsWeekend){// Apply the weekend style to the weekend dates.e.Cell.ApplyStyle(weekendStyle);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。