Click here to Skip to main content
Click here to Skip to main content

A Professional Calendar/Agenda View That You Will Use

By , 27 Aug 2009
Rate this:
Please Sign up or sign in to vote.

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)

About the Author

Jose Menendez Póo
Team Leader
Mexico Mexico
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

Comments and Discussions

 
QuestionAGREGAR A VB 2010 PinmemberMember 107222698-Apr-14 9:42 
Questiontransaction database PinprofessionalMember 104378827-Apr-14 16:38 
QuestionHow to add new fields on items.xml Pinmemberdealwi8me1-Apr-14 21:57 
QuestionSame appointment all the months Pinmemberpazzokli22-Mar-14 2:17 
QuestionFine Control, but a small question / request PinmemberFrana4e14-Mar-14 7:27 
QuestionHow to get the second picture month view PinmemberMember 28788688-Mar-14 15:11 
BugNullReferenceException on AddRange PinmemberbluishJoe53-Mar-14 21:10 
QuestionSIR can you please answer my question!! PinmemberMember 1039774218-Feb-14 16:37 
QuestionShowWeekNumbers PinmemberNorbert Wermuth3-Feb-14 22:53 
AnswerRe: ShowWeekNumbers Pinmembersteffen_dec16-Mar-14 0:57 
QuestionStart Hour and End Hour PinmemberMember 82625003-Feb-14 1:04 
QuestionHow to add a ToolTip on Item Calendar? Pinmemberserraiello1214-Jan-14 20:45 
Questionstarting / ending hour Pinmembermaani033120211846-Jan-14 19:45 
QuestionStarting Hour Pinmembermaani033120211846-Jan-14 18:56 
BugResizing PinmemberMember 1043622820-Dec-13 8:51 
QuestionChange time to 24h in moth view PinmemberJosip8414-Dec-13 11:15 
QuestionAll Day and Multiple days PinmemberMember 102739577-Dec-13 10:09 
QuestionError On Design Of Demo Form PinmemberMember 104497366-Dec-13 5:23 
QuestionCannot d/load demo! What am I missing? PinmemberMember 1040037414-Nov-13 3:55 
AnswerRe: Cannot d/load demo! What am I missing? PinmemberNimrod Eiloni14-Nov-13 5:22 
GeneralRe: Cannot d/load demo! What am I missing? PinmemberMember 1040037414-Nov-13 8:12 
QuestionHow to insert the calendar to a winform PinmemberNimrod Eiloni13-Nov-13 18:46 
AnswerRe: How to insert the calendar to a winform PinmemberMaffi198618-Dec-13 11:29 
QuestionChange de AM - PM in the time side list. PinmemberMember 103864218-Nov-13 9:54 
QuestionConnecting to ms sql PinmemberMember 95520767-Nov-13 13:52 
AnswerRe: Connecting to ms sql PinmemberMember 95520768-Nov-13 6:41 
GeneralRe: Connecting to ms sql Pinmembermaani033120211846-Jan-14 0:12 
GeneralRe: Connecting to ms sql PinmemberMember 95520766-Jan-14 2:42 
GeneralRe: Connecting to ms sql PinmemberClaudio Pérez Bocanegra16-Jan-14 6:57 
GeneralRe: Connecting to ms sql Pinmemberjomachi3-Feb-14 8:33 
QuestionAdding calendar items from outlook Pinmemberpendaz847-Nov-13 0:07 
QuestionGreat work! But last row of calendar not adding appointment. PinmemberMember 834234022-Oct-13 4:58 
QuestionRemove selected item PinmemberGabee813-Oct-13 3:37 
QuestionMonth View error PinmemberGabee811-Oct-13 1:13 
QuestionDouble-Click event doesn't care if AllowNew is false Pinmemberdlucre2-Oct-13 19:03 
Questionstart the calender at the hour we want PinmemberMember 1029652630-Sep-13 4:30 
AnswerRe: start the calender at the hour we want PinmemberMember 82625003-Feb-14 1:05 
SuggestionMaximumViewDays throws exception PinmemberVader_Oz20-Sep-13 3:46 
QuestionEvent text not full visible Pinmemberflexo_ch18-Sep-13 21:08 
QuestionHow... PinmemberVader_Oz16-Sep-13 22:24 
AnswerRe: How... PinmemberFranCastillo17-Sep-13 6:00 
GeneralMy vote of 5 PinmemberFranCastillo16-Sep-13 4:33 
GeneralGreat control despite of minor errors I found PinmemberMember 365438212-Sep-13 21:07 
QuestionSet_ViewEnd clearing and reloading items on ALL Calendars PinmemberThandius4-Sep-13 6:45 
QuestionCreating a title box above the Day Headers PinmemberThandius30-Aug-13 8:10 
QuestionHow to change view to ShortMode? Pinmemberleonardo201021-Aug-13 13:41 
BugIncorrect cursor position when hit testing for context menu PinmemberMember 1017880220-Aug-13 4:07 
QuestionIf refresh calendar items keep getting smaller PinmemberPacman8919-Aug-13 3:33 
AnswerRe: If refresh calendar items keep getting smaller Pinmemberctm5000020-Aug-13 23:40 
GeneralRe: If refresh calendar items keep getting smaller PinmemberPacman8928-Aug-13 1: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 | Mobile
Web03 | 2.8.140421.2 | Last Updated 27 Aug 2009
Article Copyright 2009 by Jose Menendez Póo
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid