65.9K
CodeProject 正在变化。 阅读更多。
Home

日历规划器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.79/5 (41投票s)

2011年9月16日

CPOL

3分钟阅读

viewsIcon

173093

downloadIcon

19251

一个类似于 Outlook 日历控件的控件

WeekPlanner/Calendar.png

引言

在我的工作中,我需要一个 .NET WinForms 控件,它允许从数据库获取数据并像 Outlook 日历规划器中那样对其进行操作(添加、删除、拖动项目),但有一些差异。 并非在控件的左侧显示时间信息,而是需要从数据库中检索和显示列数据。 我找到了一些类似的控件:您将使用的专业日历/议程视图日历 DayView 控件日、周和月日历控件,但它们都没有满足我的要求。 对于商业组件,我不想付费。 所以我创建了自己的日历规划器。

控件功能

该控件由带有项目的行和左侧的列组成,您可以从下面的屏幕截图看到

WeekPlanner/CalendarRow.png

一行中的每个项目都可以单独更新。 您可以更改背景颜色,指定文本,拖动它,更改日期等。在日历的左侧,您可以添加包含一些文本的列,它可能用于从不同的数据源(例如,从数据库或 XML 文件)检索数据。 该组件可以以两种模式显示日期间隔:每日和每周。 在每日模式下,每个日期显示星期几的名称,如上图所示。 在每周模式下,日期显示周数,如下面的屏幕截图所示。 控件支持行嵌套。 为此,您可以将一行设置为可折叠。

WeekPlanner/CalendarRow2.png

背景

为了管理控件的行为,您可以使用不同的属性。 其中一些是

  • CurrentDate:获取/设置日历日期
  • DatesIntervalMode:在标头中显示日期间隔的可能模式
  • DayCount:日历要显示的日期数
  • GridCellHeight:每行的高度
  • HeaderStylemode:填充背景标题的可能模式
  • IsAllowedDraggingBetweenRows:允许/禁止在行之间拖放项目
  • ItemHeight:每个项目的高度
  • LeftMargin:左列的边距
  • SelectedRowindex:获取所选行的索引
  • SelectedItem:获取选定的项目
  • Rows:日历的行列表
  • Items:行的项目列表
  • Columns:控件左侧的列
  • IsAllowedTreeViewDrawing:启用/禁用类似树视图的线条绘制

事件

  • ItemClick:单击项目时发生
  • ItemDoubleClick:双击项目时发生
  • RowClick:单击行时发生
  • RowDoubleClick:双击行时发生
  • ItemDatesChanged:项目日期范围发生变化时发生
  • ItemTextEdited:编辑项目文本时发生

Using the Code

该控件不支持数据绑定,因此您必须编写自己的逻辑来保存和获取带有项目的行。

该控件的每一行都是类 WeekPlannerRow,具有不同的属性(例如 BackColorName 等)。 日历由类 WeekPlannerRowCollection: List<WeekPlannerRow> 中的行列表组成。

反过来,每一行都包含一个项目列表 WeekPlannerItemCollection:List<WeekPlannerItem>。 一个项目是类 WeekPlannerItem,具有一些属性:StartDateEndDateSubjectBackColor 等。

此外,每一行都有列,所有相关方法都可以在 DataColumns 类中找到。 该类包含一个类数组 DataValue,具有以下属性:Name(列名)、Width(列宽)和 Text(列标题的文本)。 此外,DataValue 类有一个列表 ValueColorCollection : List<weekplanneritem>ValueColor<weekplanneritem> 类的目的是以不同的颜色显示列中的文本信息。

为了添加列,您可以使用此代码

weekPlanner1.Columns.Add("ColumnName1", string.Empty, 150);
weekPlanner1.Columns.Add("ColumnName2", string.Empty, 150);
// 150 is the width of column

添加行的方式如下

// creates new instance of the List of Items, which will be added to the row
var itemCollection = new WeekPlannerItemCollection(); 
// creates new instance of item, which will be added to the row's items list
var item = new WeekPlannerItem(); 
item.StartDate = DateTime.Now.AddDays(0); 
item.EndDate = DateTime.Now.AddDays(0); 
item.Subject = "1 date"; 
item.BackColor = Color.YellowGreen; 
// adds item to the list
itemCollection.Add(item); 
// creates new instance of the columns, which will be added to the row
var ColumnRows = new DataColumns(weekPlanner1.Calendar); 
// adds random text to the column ColumnName1,
// which was created in the code snippet above
ColumnRows["ColumnName1"].Data.Add(Guid.NewGuid().ToString().Substring(0, 8)); 
// adds another random text to the column ColumnName1 
ColumnRows["ColumnName1"].Data.Add(Guid.NewGuid().ToString().Substring(0, 8)); 
// adds random text to the column ColumnName2
ColumnRows["ColumnName2"].Data.Add(Guid.NewGuid().ToString().Substring(0, 8)); 
// adds row to the calendar
weekPlanner1.Rows.Add(ColumnRows, itemCollection);

这是添加项目的代码

// gets calendar rows collection
var rows = weekPlanner1.Rows; 
// finds selected row by index
var row = rows.ElementAt(weekPlanner1.SelectedRowIndex); 
// creates new instance of item, which will
// be added to the row's items list
var item = new WeekPlannerItem(); 
item.StartDate = DateTime.Now.AddDays(0); 
item.EndDate = DateTime.Now.AddDays(0); 
item.Subject = "Example"; 
item.BackColor = Color.YellowGreen; 
// adds item to the row
row.Items.Add(item);

历史

  • 2011 年 8 月 9 日
    • 首次发布
  • 2011 年 8 月 20 日
    • 添加了新事件 OnItemDatesChangedOnItemTextEdited
  • 2011 年 9 月 4 日
    • 添加了新事件 OnItemMouseHoverOnItemMouseLeave
  • 2011 年 10 月 11 日
    • 现在,每一行单独增加其高度
  • 2013 年 4 月 24 日
    • 添加了新属性 IsAllowedTreeViewDrawing,事件 OnRowLeftColumnClick
    • 添加了新的行属性 IsVisibleIsExpandedAncestorNameIsCollapsible
© . All rights reserved.