Click here to Skip to main content
Licence CPOL
First Posted 30 Nov 2009
Views 33,634
Downloads 2,461
Bookmarked 52 times

Culture Aware Month Calendar and DatePicker

By | 22 Nov 2011 | Article
A month calendar and date picker with culture awareness.

Introduction

As you can see in the screenshot, this is a month calendar control and date picker control, a little like the built-in controls from Microsoft. It's not implemented using WPF, but is for WinForms applications.

Background

While developing my own calendar/appointment calendar similar to the one in Outlook 2007, I needed a month calendar. There are many good ones out there, but not quite the way I needed. So, I started to develop my own. Along the way, I implemented not only a month calendar but also a date picker control.

Also, I thought, why not implement culture awareness into the controls, so people in different countries can use it too. That was more complex than I thought, and I ran into many problems regarding culture support, including some nasty bugs in the framework; more in detail later.

I thought to share the results here, because CodeProject has offered me solutions to my problems several times.

Class Diagram

Features

  • culture aware - meaning you can set the used culture and calendar
  • adjustable number of visible sub-calendars via the property CalendarDimensions
  • fully customizable in regard to colors and fonts
  • you can load or save the color table
  • adjustable RTL or LTR layout
  • can adjust whether to use shortest or abbreviated day names
  • can adjust whether to show week header and footer
  • adjustable non working days (for work week selection mode)
  • adjustable first day of week
  • adjustable day names (full/abbreviated/shortest)
  • adjustable month names (full/abbreviated)
  • adjustable month/day string patterns
  • supports settable min/max date
  • scrolling with mouse wheel
  • different selection modes (single day/work week/full week/manual)
  • adjustable maximum selectable days in manual selection mode
  • bolded days collection
  • new: extended bolded days (category and corresponding color definition)
  • new: the manual selection mode now supports selection of multiple ranges with the CTRL key
  • adjustable scroll change (how many months to scroll when clicking an arrow)
  • date picker control supports keyboard navigation
  • disabled state support

Using the Code

Simply add the control project to your existing solution and reference it. The control is built with VS 2008, and the targeted framework version is 3.5.

Points of Interest

Regarding the culture awareness of the controls, I must admit that I cannot guarantee that all is correctly displayed and calculated. If there are errors, please feel free to post a message.

As mentioned earlier, while implementing culture awareness, I encountered some nasty bugs in the framework. One of these is as follows:

Every System.Globalization.Calendar implementation has a method GetWeekOfYear(). And, every calendar has a property MinSupportedDateTime. If you want to get the week of the year from that date, then in some cases, you get an ArgumentOutOfRangeException although the date passed to the method is a valid one.

Calendar implementations affected are Hijri-, UmAlQura-, Hebrew-, Persian-, JulianCalendar, and all implementations of EastAsianLunisolarCalendar.

Another bug is in the JapaneseCalendar where the method GetYear() returns 0 for a specific date range.

It is highly possible that I didn't find all bugs regarding the calendar implementations, so if you set an unusual calendar like the JapaneseLunisolarCalendar, it can happen that exceptions are thrown.

Customization of the MonthControl

The rendering of the control is handled by a separate renderer class. If you want to customize the drawing, then you can implement your own renderer by deriving from the abstract base class or the default renderer.

That's also true for the color table class which implements IXmlSerializable.

Adjusting the MonthCalendar Control

One point you have to be aware of is that only non-neutral cultures can be set. The reason for that is, neutral cultures have no value for the DateTimeFormat property, which I need for different reasons.

To adjust the visible sub-month views, you can directly set the CalendarDimensions property, or use the designer.

Using the property ColorTable, you can adjust the used colors, including whether to use gradient colors and the gradient mode or not.

Adjusting the DatePicker Control

The date in the text box part of the picker is displayed using the short date pattern, which you can also adjust. This pattern is also used when parsing a date.

The picker control has the CheckDate event where you can check and set if the entered date is a valid one. If not valid, then the date is displayed using the colors of the InvalidBackColor and InvalidForeColor properties.

Another important property is ClosePickerOnDayClick. With it, you can adjust whether the picker closes when clicking on a day, regardless of whether the day is already selected or not.

Extended Bolded Dates

The DatePicker and MonthCalendar controls now have two new properties:

  • BoldedDateCategoryCollection
  • BoldedDatesCollection

The first collection holds the definitions of categories for bolded dates, and the second the bolded dates with corresponding category.

The original property for defining bold dates works as before.

Events

The month control has the following important events:

  • DateChanged: Occurs when the month or year is changed via the context menu
  • DateClicked: Occurs when a day is clicked in the 'Day' selection mode
  • DateSelected: Occurs when a date or date range is selected
  • SelectionExtendEnd: Occurs when the selection extension ended

The date picker has an important event:

  • ValueChanged: Occurs when the date value changes

Both controls have one event in common:

  • ActiveDateChanged: Occurs when the mouse is over a date

History

  • 1st December, 2009: Initial post.
  • 12th October, 2011: Extended bold dates with categories.
  • 22nd November, 2011: Extended selection to support multiple ranges.

License

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

About the Author

Thomas Duwe



Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionVery nice control! PinmemberMiMoe22:08 3 Apr '12  
AnswerRe: Very nice control! PinmemberThomas Duwe23:32 3 Apr '12  
GeneralRe: Very nice control! PinmemberMiMoe5:00 4 Apr '12  
GeneralRe: Very nice control! PinmemberMiMoe2:07 5 Apr '12  
GeneralRe: Very nice control! PinmemberThomas Duwe5:27 5 Apr '12  
QuestionDo you think I can integrated with Devexpress DatePicker PinmemberAlmatrodi20:02 15 Jan '12  
AnswerRe: Do you think I can integrated with Devexpress DatePicker PinmemberThomas Duwe21:37 15 Jan '12  
GeneralMy vote of 5 Pinmemberjpalexa@yahoo.com5:18 24 Nov '11  
GeneralRe: My vote of 5 PinmemberThomas Duwe21:49 24 Nov '11  
QuestionMultiple date ranges? Pinmemberjpalexa@yahoo.com23:58 21 Nov '11  
AnswerRe: Multiple date ranges? PinmemberThomas Duwe2:24 22 Nov '11  
GeneralRe: Multiple date ranges? Pinmemberjpalexa@yahoo.com5:18 24 Nov '11  
QuestionFont and Color properties for individual TextBold Pinmembere54906260:06 11 Oct '11  
AnswerRe: Font and Color properties for individual TextBold PinmemberThomas Duwe1:47 12 Oct '11  
QuestionWeek Number Problem [modified] Pinmemberscubaduba3:20 26 Sep '11  
AnswerRe: Week Number Problem PinmemberThomas Duwe9:23 26 Sep '11  
QuestionPickerBolderDates ? PinmemberMember 81890188:47 25 Aug '11  
AnswerRe: PickerBolderDates ? PinmemberThomas Duwe23:01 25 Aug '11  
GeneralRe: PickerBolderDates ? PinmemberMember 818901823:06 25 Aug '11  
GeneralRe: PickerBolderDates ? PinmemberThomas Duwe23:25 25 Aug '11  
GeneralRe: PickerBolderDates ? PinmemberMember 81890182:23 26 Aug '11  
GeneralRe: PickerBolderDates ? PinmemberThomas Duwe2:47 26 Aug '11  
GeneralRe: PickerBolderDates ? [modified] PinmemberMember 81890182:54 26 Aug '11  
GeneralRe: PickerBolderDates ? PinmemberThomas Duwe22:07 28 Aug '11  
GeneralRe: PickerBolderDates ? PinmemberMember 818901822:17 28 Aug '11  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 22 Nov 2011
Article Copyright 2009 by Thomas Duwe
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid