Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > vs2005中Calendar控件的一些使用
【标  题】:vs2005中Calendar控件的一些使用
【关键字】:vs2005,Calendar
【来  源】:http://blog.csdn.net/featheast/archive/2006/07/17/933853.aspx

vs2005中Calendar控件的一些使用

Your Ad Here

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

// 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 集合,可操作该集合以将内容添加到日中。

自定义个别日的外观

  1. Calendar 控件的 DayRender 事件创建一个方法。该事件应该具有以下签名:
    ' Visual Basic
    Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
    ' Additional code here
    End Sub
    
    // C#
    private void Calendar1_DayRender (object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
    {
       // Additional code here
    }
  2. 在该方法中,设置通过 DayRenderEvent 参数可用的 Cell 对象的属性,如下例所示:
    ' Visual Basic
       If (e.Day.IsToday) Then
          e.Cell.BackColor = System.Drawing.Color.Red
       End If
    
    // C#
       if (e.Day.IsToday)
       {
          e.Cell.BackColor = System.Drawing.Color.Red;
       }

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

' Visual Basic
Public 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))) Then
      e.Cell.ApplyStyle(vacationStyle)
   ElseIf (e.Day.IsWeekend) Then
      e.Cell.ApplyStyle(weekendStyle)
   End If
End 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);
   }
}   


 

藏于系统的抗毒英雄:【上一篇】
使用C#求解N皇后问题。:【下一篇】
【相关文章】
  • VS2005 常用快捷键
  • 使用封装自System.Windows.Forms.Form 类派生的Windows窗体,在VS2005设计时,一些无关的属性会被调用问题分...
  • 在VS2005中的第一个J#程序
  • VS2005 Express 让人大失所望
  • Torque的VS2005编译 (compiling Torque by VS2005)
  • VS2005为什么会有两个AssemblyInfo
  • 管中窥豹:VS2005与VC6的差别
  • 使用vs2005(vc8)编译log4cpp-0.3.5rc3
  • 在未安装VS2005的机器上运行VS2005编译的C++程序
  • 用Emacs做日程安排及calendar使用方法
  • 【随机文章】
  • WSS3.0入门第二天
  • 18黑客<谁动了我电脑>附录 三
  • 用js是实现的网页日期选取
  • 从本地下载软件包,快速安装debian [菜鸟经验,上手]
  • 经典算法-C#四种排序算法
  • SIMD指令集支持
  • Appfuse里数据库驱动的下拉菜单
  • 用haskell求解一个组合问题
  • unix中日期使用误区
  • 宽带贵族--光纤局域网FTTB
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.