Click here to Skip to main content
11,637,415 members (60,862 online)
Click here to Skip to main content

A Professional Calendar/Agenda View That You Will Use

, 27 Aug 2009 LGPL3 965.8K 62.4K 616
Rate this:
Please Sign up or sign in to vote.
An Outlook style calendar view with appointments and all-day events, and multiple day view support.

Introduction

As you can see in the screenshots, this is a fully capable calendar view for specifying appointments and all day events. It has several features so you can control what happens inside, like item blocking and item-oriented events.

It's 100% managed code! No resources; as with most of my projects, you can just include all the source files into your project to make it work.

Background

I found a couple of controls like this, but as always, they don't meet my needs, so here I come. It performs fairly well, but mostly will depend on your implementation - I'll explain that in the next section.

Using the Code

To add a calendar to your form, just drag the Calendar control. It's located under the System.Widows.Forms.Calendar namespace.

When to Use it

It can be used to display any information that is based on a date, not just appointments and meetings. Think about it, on what ugly control do you display your system log?

Controlling the View

The view of the Calendar is given by a date range provided by the ViewStart and ViewEnd properties. Depending on the number of days between these two dates, the calendar will draw them.

The Calendar can show days in two modes: Expanded and Short (see Calendar.DaysMode). Expanded is the style of the first screenshot: the days are shown in a column, and items are placed in the time they belong; while the Short mode (second screenshot) shows days on week rows, and items are shown in a more compact way.

An important property here is MaximumFullDays (by default, 8). This property indicates that when you specify a view of 8 days or less, the days will be shown in Expanded mode. Any more days will be displayed in Short mode.

Feeding Items to the Calendar

The calendar tells you when to add items to it with the LoadItems event. In that event, you should manage to bring information to display in the calendar, by adding items to the Items collection of the calendar. The event is raised every time the view changes. I strongly suggest that you use caching and not query the database every time this event is raised, because performance will be severely affected.

The demo project in the source loads items from a memory array, so it's not the best example.

private void calendar1_LoadItems(object sender, CalendarLoadEventArgs e)
{
    //Load items whose date range intersects e.DateStart and e.DateEnd
    foreach(CalendarItem item in loadedItems)
    {
         calendar1.Items.Add(item);
    }
 
    //Or even better....
    calendar1.Items.AddRange(loadedItems);
}

I strongly suggest to add items that only intersect with the calendar's view range. You can learn how to check date intersection by taking a look at the implementation of DateIntersects, in Calendar:

public static bool DateIntersects(DateTime startA, DateTime endA, 
                                  DateTime startB, DateTime endB)
{
     //Don't forget to check dates this way in your database queries!
     return startB < endA && startA < endB;
}

Events

Since you can explore other members using IntelliSense I list events here; they are important because they let you control your application of the Calendar.

  • DayHeaderClick: Occurs when a day header is clicked.
  • ItemClick: Occurs when an item is clicked.
  • ItemCreated: Occurs when an item is successfully created.
  • ItemCreating: Occurs when the user is about to create an item. It can be cancelled.
  • ItemDatesChanged: Occurs when the date range of an item changes.
  • ItemDeleted: Occurs when an item is successfully deleted.
  • ItemDeleting: Occurs when the user is about to delete an item. It can be cancelled.
  • ItemDoubleClick: Occurs when an item is double clicked.
  • ItemMouseHover: Occurs when the mouse is placed over an item.
  • ItemSelected: Occurs when an item is selected.
  • ItemTextEdited: Occurs when an item's text is edited by the user.
  • ItemTextEditing: Occurs when the user is trying to edit an item's text. It can be cancelled.
  • LoadItems: Occurs when the Calendar view is changed.

Some Nice Features

Items Overlapping

When items intersect in their date ranges, there's a nice algorithm that performs a layout to accommodate them. Give it a try.

Item Coloring

Although a Renderer takes charge of drawing items, you can specify the background colors and borders to items individually.

Even better, you can use the ApplyColor method (in CalendarItem) to an item, and the code will take charge of shading colors for the background, border, and text.

In the demo application, use the calendar's contextual menu to apply coloring to items.

TimeScale

You can choose between options of time scaling, though, the default is 30 minutes like Outlook's calendar. Here is a sample of a 15 minutes timescale.

In the demo application, use the calendar's contextual menu to choose different timescale options.

MonthViewControl

Don't you absolutely hate the way the MonthCalendar control behaves on your UI? Well, here is the solution. Now, the project contains a control called MonthView, which looks like the Outlook calendar's view, fully customizable, and it does not force the size of the control; the visualization of months will depend on the size of the container.

Some interesting properties of the control are:

  • FirstDayOfWeek - To change what day your weeks start with.
  • ItemPadding - To set the padding of the internal items, so you can make a compact or not so much view.
  • SelectionMode - Manual, Day, Week, WorkWeek, and Month.
  • WorkWeekStart - To specify what day the work-weeks start with.
  • WorkWeekEnd - To specify what day the work-weeks end with.

History

  • Aug 02 2009: First release.
  • Aug 05 2009
    • License changed to LGPL.
    • Added the FirstDayOfWeek property to change what day your weeks start with.
    • Added the Tag property to CalendarItem.
  • Aug 27 2009
    • Added minute exact indicators.
    • Added keyboard arrows support.
    • Added Image and ImageAlign properties to CalendarItem for adding images to items.
    • Added Pattern and PatternColor properties to CalendarItem for marking items with textures.
    • Added the MonthView control to have a nice calendar control.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

Jose Menendez Póo
Team Leader
Mexico Mexico
I'm in game programming now: https://itunes.apple.com/us/app/ugly-aliens-training-center/id859271884?ls=1&mt=8

Jose Manuel Menéndez Poó

- I've been programming Windows and Web apps since 1997.
- My greatest concern nowadays is user interface usability.

Questions and stuff by twitter: @menendezpoo

Blog
menendezpoo.com

You may also be interested in...

Comments and Discussions

 
QuestionShows Appointment Count in monthview Pin
udan29-Nov-12 16:13
memberudan29-Nov-12 16:13 
AnswerRe: Shows Appointment Count in monthview Pin
helloah29-Nov-12 16:22
memberhelloah29-Nov-12 16:22 
QuestionAdd Columns to calendar tool Pin
rose lindo28-Nov-12 22:18
memberrose lindo28-Nov-12 22:18 
QuestionHow to add item to your calender Pin
benson ndirangu28-Nov-12 19:32
memberbenson ndirangu28-Nov-12 19:32 
AnswerRe: How to add item to your calender Pin
Pyloz22-Mar-13 11:54
memberPyloz22-Mar-13 11:54 
QuestionLooks great, but I'm not sure how to add it Pin
helloah27-Nov-12 22:03
memberhelloah27-Nov-12 22:03 
QuestionFantastic Job and just a little question Pin
Member 958728211-Nov-12 8:24
memberMember 958728211-Nov-12 8:24 
AnswerRe: Fantastic Job and just a little question Pin
Member 992793022-Mar-13 4:11
memberMember 992793022-Mar-13 4:11 
QuestionDuda de tu calendario Pin
Member 92681359-Nov-12 9:04
memberMember 92681359-Nov-12 9:04 
QuestionHow to change Time slot? Pin
Member 78857023-Oct-12 5:01
memberMember 78857023-Oct-12 5:01 
AnswerRe: How to change Time slot? Pin
Member 78857023-Oct-12 5:16
memberMember 78857023-Oct-12 5:16 
QuestionItem is lossed when i'm scroll mouse Pin
quoctien8221-Oct-12 22:25
memberquoctien8221-Oct-12 22:25 
AnswerRe: Item is lossed when i'm scroll mouse Pin
Member 1007456924-May-13 4:24
memberMember 1007456924-May-13 4:24 
QuestionSet Item to Highlighted calendar range Pin
teberle18-Oct-12 10:13
memberteberle18-Oct-12 10:13 
How can I select a time range in the calendar control use that as my start/end times?
QuestionHow to get the first and last dates of the MonthView control? Pin
superkuton17-Oct-12 4:19
membersuperkuton17-Oct-12 4:19 
SuggestionA finger in the nose ScrollBar Pin
Arnaud Dovi17-Oct-12 3:16
memberArnaud Dovi17-Oct-12 3:16 
Questionpopulate control from database and update database from control. Pin
kyrsoronas16-Oct-12 3:02
memberkyrsoronas16-Oct-12 3:02 
AnswerRe: populate control from database and update database from control. Pin
hab012311-Nov-12 1:05
memberhab012311-Nov-12 1:05 
Questionhow to programatically select data from Calendar control Pin
Member 22817716-Oct-12 3:06
memberMember 22817716-Oct-12 3:06 
BugBug in this control Pin
chowdary200526-Sep-12 23:10
memberchowdary200526-Sep-12 23:10 
QuestionEditModeItem is showing as null Pin
chowdary200525-Sep-12 20:36
memberchowdary200525-Sep-12 20:36 
AnswerRe: EditModeItem is showing as null Pin
chowdary200526-Sep-12 23:10
memberchowdary200526-Sep-12 23:10 
Questionquestion about the data Pin
dovaghaar15-Sep-12 5:53
memberdovaghaar15-Sep-12 5:53 
AnswerRe: question about the data Pin
Dieter Vander Donckt16-Sep-12 9:31
memberDieter Vander Donckt16-Sep-12 9:31 
GeneralRe: question about the data Pin
dovaghaar17-Sep-12 7:53
memberdovaghaar17-Sep-12 7:53 
GeneralRe: question about the data Pin
Dieter Vander Donckt17-Sep-12 8:51
memberDieter Vander Donckt17-Sep-12 8:51 
GeneralRe: question about the data Pin
dovaghaar17-Sep-12 9:08
memberdovaghaar17-Sep-12 9:08 
BugCalendarItem Locked property not working Pin
yonan223611-Sep-12 17:44
memberyonan223611-Sep-12 17:44 
GeneralRe: CalendarItem Locked property not working Pin
Dieter Vander Donckt11-Sep-12 22:56
memberDieter Vander Donckt11-Sep-12 22:56 
GeneralRe: CalendarItem Locked property not working Pin
yonan223611-Sep-12 23:55
memberyonan223611-Sep-12 23:55 
QuestionChange working days & hours calendar background Pin
Dieter Vander Donckt10-Sep-12 2:09
memberDieter Vander Donckt10-Sep-12 2:09 
AnswerRe: Change working days & hours calendar background Pin
omar sallam22-Sep-12 2:41
memberomar sallam22-Sep-12 2:41 
GeneralRe: Change working days & hours calendar background Pin
Dieter Vander Donckt22-Sep-12 3:50
memberDieter Vander Donckt22-Sep-12 3:50 
GeneralRe: Change working days & hours calendar background Pin
omar sallam22-Sep-12 20:46
memberomar sallam22-Sep-12 20:46 
QuestionGreat calendar control Pin
Dieter Vander Donckt1-Sep-12 10:23
memberDieter Vander Donckt1-Sep-12 10:23 
GeneralMy vote of 5 Pin
Eddy Vluggen31-Aug-12 1:32
memberEddy Vluggen31-Aug-12 1:32 
Questionhow select one item? Pin
Alejandro Diaz Torres29-Aug-12 0:33
memberAlejandro Diaz Torres29-Aug-12 0:33 
AnswerRe: how select one item? Pin
Dieter Vander Donckt1-Sep-12 23:36
memberDieter Vander Donckt1-Sep-12 23:36 
GeneralRe: how select one item? Pin
alexdt3-Sep-12 0:46
memberalexdt3-Sep-12 0:46 
QuestionHow to get target date on drag drop? Pin
Member 100512728-Aug-12 5:39
memberMember 100512728-Aug-12 5:39 
AnswerRe: How to get target date on drag drop? Pin
Member 81903285-Oct-12 3:08
memberMember 81903285-Oct-12 3:08 
QuestionNo way to hide non working hours? Pin
Member 100512727-Aug-12 11:00
memberMember 100512727-Aug-12 11:00 
SuggestionRe: No way to hide non working hours? Pin
Henry Kim4-Oct-12 18:05
memberHenry Kim4-Oct-12 18:05 
GeneralRe: No way to hide non working hours? Pin
pianocomposer19-May-14 7:18
memberpianocomposer19-May-14 7:18 
QuestionHow to implement Pin
Chrenner16-Aug-12 3:34
memberChrenner16-Aug-12 3:34 
AnswerRe: How to implement Pin
Chrenner18-Aug-12 0:45
memberChrenner18-Aug-12 0:45 
GeneralRe: How to implement Pin
Santa's Little Helper18-Aug-12 4:09
memberSanta's Little Helper18-Aug-12 4:09 
GeneralRe: How to implement Pin
Chrenner19-Aug-12 4:42
memberChrenner19-Aug-12 4:42 
QuestionQuestion on PassingItems Pin
Juvica14-Aug-12 2:06
memberJuvica14-Aug-12 2:06 
AnswerRe: Question on PassingItems Pin
Juvica15-Aug-12 23:57
memberJuvica15-Aug-12 23:57 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 27 Aug 2009
Article Copyright 2009 by Jose Menendez Póo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid