Click here to Skip to main content
16,019,876 members
Articles / Desktop Programming / Windows Forms

Calendar Planner

Rate me:
Please Sign up or sign in to vote.
4.79/5 (42 votes)
6 May 2013CPOL3 min read 170.2K   19.2K   187   64
A control similar to the Outlook Calendar control
This is a .NET WinForms control which allows to fetch data from database and manipulate it like in Outlook Calendar Planner, but with some differences.

WeekPlanner/Calendar.png

Image 2

Introduction

During my job, I was in need of a .NET WinForms control which would allow to fetch data from the database and manipulate it like in Outlook Calendar Planner (add, delete, drag items), but with a few differences. Instead of showing the time information in the left side of the control, it was necessary to retrieve and show columnar data from the database. I found some similar controls: A Professional Calendar/Agenda View That You Will Use, Calendar DayView Control, and Day, Week, and Month Calendar Controls, but none of them met my requirements. For commercial components, I didn't want to pay. So I created my own Calendar Planner.

Control Features

The control consists of rows with items and columns on the left side, as you can see from the screenshot below:

WeekPlanner/CalendarRow.png

Each item in a row can be updated individually. You can change the background color, specify text, drag it, change date, etc. In the left side of the calendar, you can add columns with some text, it may be used in order to retrieve data from different data sources (for example, from DB or XML file). The component can show date intervals in two modes: daily and weekly. In daily mode, each date shows day name, as in the screenshot above. In weekly mode, the date shows the week number, as in the screenshot below. Control supports rows nesting. For this, you can set a row as collapsible.

WeekPlanner/CalendarRow2.png

Background

In order to manage the behaviour of the control, you can use different properties. Some of them are:

  • CurrentDate: gets/sets calendar date
  • DatesIntervalMode: possible modes to show date intervals in the header
  • DayCount: day count to show by the calendar
  • GridCellHeight: height of each row
  • HeaderStylemode: possible modes to fill background header
  • IsAllowedDraggingBetweenRows: allows/disallows item drag/drop between rows
  • ItemHeight: height of each item
  • LeftMargin: margin of left column
  • SelectedRowindex: gets the index of the selected row
  • SelectedItem: gets the selected item
  • Rows: list of rows of the calendar
  • Items: list of items of the row
  • Columns: columns in the left side of the control
  • IsAllowedTreeViewDrawing: enables\disables lines drawing like in treeview

Events:

  • ItemClick: occurs when an item is clicked
  • ItemDoubleClick: occurs when an item is double-clicked
  • RowClick: occurs when a row is clicked
  • RowDoubleClick: occurs when a row is double-clicked
  • ItemDatesChanged: occurs when an item date range has changed
  • ItemTextEdited: occurs when an item text is edited

Using the Code

The control doesn't support data binding, so you must write your own logic for rows with items saving and fetching.

Each row of the control, this is the class WeekPlannerRow with different properties (such as BackColor, Name, and others). The calendar consists of a list of rows in the class WeekPlannerRowCollection: List<WeekPlannerRow>.

In turn, each row contains a list of items WeekPlannerItemCollection:List<WeekPlannerItem>. An item is the class WeekPlannerItem with some properties: StartDate, EndDate, Subject, BackColor, and others.

Also, each row has columns, and all related methods can be found in the DataColumns class. This class contains an array of classes DataValue with properties like: Name (name of column), Width (width of column), and Text (text for column header). Also, the DataValue class has a list ValueColorCollection : List<weekplanneritem>. The purpose of the ValueColor<weekplanneritem> class is to show text information in a column in different colors.

In order to add columns, you can use this code:

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

Rows adding happens this way:

C#
// 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);

Here is the code for items adding:

C#
// 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);

History

  • 9th August, 2011
    • Initial release
  • 20th August, 2011
    • Added new events OnItemDatesChanged, OnItemTextEdited
  • 4th September, 2011
    • Added new events OnItemMouseHover, OnItemMouseLeave
  • 11th October, 2011
    • Now each row increases its height individually
  • 24th April, 2013
    • Added new property IsAllowedTreeViewDrawing, event OnRowLeftColumnClick
    • Added new row properties IsVisible, IsExpanded, AncestorName, IsCollapsible

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Other Other
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLoad from database Pin
Member 1096478217-Jan-19 14:01
Member 1096478217-Jan-19 14:01 
QuestionRow height fitting Pin
Micropedia10-Jul-17 9:25
Micropedia10-Jul-17 9:25 
QuestionPrint this Planner? Pin
Member 1261191728-Feb-17 2:04
Member 1261191728-Feb-17 2:04 
AnswerRe: Print this Planner? Pin
Member 1437211817-Jun-19 10:08
Member 1437211817-Jun-19 10:08 
QuestionHow to use HorizontalScroll in Calendar Plenner Pin
nhatnguyen1234567898-May-16 17:36
nhatnguyen1234567898-May-16 17:36 
AnswerRe: How to use HorizontalScroll in Calendar Plenner Pin
lazos1326-Sep-18 3:27
lazos1326-Sep-18 3:27 
QuestionHow to create a right click event on Item? Pin
Murulimadhav112-Mar-16 21:19
Murulimadhav112-Mar-16 21:19 
QuestionRow description Pin
Alexander Tschense7-Oct-15 0:44
Alexander Tschense7-Oct-15 0:44 
QuestionDrap&Drop items Pin
Member 866508217-Sep-15 6:27
Member 866508217-Sep-15 6:27 
Questionhow to select a row by row name and how to add, edit row names? Pin
Murulimadhav13-Sep-15 21:32
Murulimadhav13-Sep-15 21:32 
Answerday names Pin
Micropedia19-Aug-15 4:42
Micropedia19-Aug-15 4:42 
I'm italian, i would change "mon" with "lun" is it possible? how?
GeneralRe: day names Pin
Member 360371822-Dec-15 4:51
Member 360371822-Dec-15 4:51 
GeneralRe: day names Pin
Member 128550078-Feb-20 19:21
Member 128550078-Feb-20 19:21 
Suggestionday language? Pin
Micropedia19-Aug-15 4:38
Micropedia19-Aug-15 4:38 
GeneralRe: day language? Pin
Member 128550078-Feb-20 19:24
Member 128550078-Feb-20 19:24 
QuestionPlease Help: How to add hours Pin
Member 1167418525-Jun-15 18:24
Member 1167418525-Jun-15 18:24 
QuestionWeekEnds Pin
Member 1167418518-Jun-15 15:54
Member 1167418518-Jun-15 15:54 
Questionupdating DB table Pin
Member 1167418515-Jun-15 17:41
Member 1167418515-Jun-15 17:41 
QuestionHow to disable moving, editing and resizing the events ? Pin
Murulimadhav128-Jan-15 0:03
Murulimadhav128-Jan-15 0:03 
QuestionMake some item unmovable/unclickable Pin
YAllTaken4-Jun-14 22:37
YAllTaken4-Jun-14 22:37 
QuestionMonth View? Pin
RRJP20-Nov-13 21:18
RRJP20-Nov-13 21:18 
AnswerRe: Month View? Pin
RRJP20-Nov-13 22:57
RRJP20-Nov-13 22:57 
QuestionBackground Color of individual Cells Pin
Member 101956688-Oct-13 5:26
professionalMember 101956688-Oct-13 5:26 
QuestionNICE WORK!! Pin
Paulo Lima2-Oct-13 8:36
Paulo Lima2-Oct-13 8:36 
Questionload from DB Pin
tony5551-Oct-13 5:02
tony5551-Oct-13 5:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.