Click here to Skip to main content
Click here to Skip to main content
Go to top

Culture Aware Month Calendar and DatePicker

, 16 Jul 2012
Rate this:
Please Sign up or sign in to vote.
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

There is a new property AllowPromptAsInput in the DatePicker control.
Setting it to true allows to enter dates including the current date separator as set in the
FormatProvider property.

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
  • new: date separator now valid input in picker control
  • 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 2010, 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.

Usage of Native Digits

Both controls have a new property :

  • UseNativeDigits

This property converts any displayed number in the controls to a string containing the
NativeDigits from the current Culture.

If setting the current Culture to Persian (fa-IR) then this culture has not arabic numerals
but persian numerals.

For instance the persian date 01.01.1390 in the format dd/MM/yyyy would then be displayed as

۰۱ / ۰۱ / ۱۳۹۰

Let me show you an image if setting a datepicker's Culture and the CultureCalendar to Persian:

Furthermore the manual input of an date uses the native digits too.

Please note that the displayed day names (probably the month names too) could be wrong,
because these values are taken from the .Net Framework CultureInfo classes, specifically the
Persian day names (in this case the shortest day names) are incorrect due to a bug/feature of
the Persian (fa-IR) CultureInfo class.

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.
  • 14th June, 2012 :
    • Implemented native digits usage
    • changed project to VS 2010 project
    • fixed bugs regarding the setting of month and day names of the FormatProvider
    • fixed bug in internal parsing/displaying of dates
    • now wheel scrolling with the mouse is prevented if the popup of the datepicker is open
    • fixed an focus issue of the datepicker control
    • some other small improvements and fixes
  • 17th June, 2012 :
    • the current date separator is now allowed as input in the DatePicker control
      valid input is e.g. 06172012, 061712, [0]6/17/[20]12 if the ShortDatePattern is set to
      MM/dd/yyyy

License

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

Share

About the Author

Thomas Duwe

Germany Germany
No Biography provided

Comments and Discussions

 
QuestionPlease tell me how to execute it. PinprofessionalSnehasish_Nandy1-Aug-14 0:26 
GeneralRe: Please tell me how to execute it. PinmemberThomas Duwe4-Aug-14 2:13 
QuestionToolstrip PinmemberMember 1007607419-Jul-14 23:45 
GeneralRe: Toolstrip PinmemberThomas Duwe20-Jul-14 2:28 
GeneralRe: Toolstrip PinmemberMember 1007607420-Jul-14 3:08 
GeneralRe: Toolstrip PinmemberThomas Duwe20-Jul-14 4:16 
QuestionDefault culture PinmemberMember 1007607419-Jul-14 8:44 
AnswerRe: Default culture PinmemberThomas Duwe20-Jul-14 2:27 
GeneralRe: Default culture PinmemberMember 1007607420-Jul-14 3:03 
AnswerRe: Default culture PinmemberThomas Duwe20-Jul-14 4:15 
QuestionMessage Error [modified] PinmemberMember 1034720124-Feb-14 4:52 
AnswerRe: Message Error Pinprofessional10347201 - Vicens Ferran24-Feb-14 8:06 
GeneralRe: Message Error PinmemberThomas Duwe24-Feb-14 20:50 
GeneralRe: Message Error Pinprofessional10347201 - Vicens Ferran25-Feb-14 5:18 
GeneralRe: Message Error Pinprofessional10347201 - Vicens Ferran25-Feb-14 6:58 
QuestionWorking with VB Net PinmemberMember 1034720120-Oct-13 10:35 
AnswerRe: Working with VB Net PinmemberThomas Duwe22-Oct-13 21:53 
GeneralRe: Working with VB Net PinmemberMember 1034720123-Oct-13 7:55 
QuestionHow to add my country lunar date? Pinmemberkhoirom1-Oct-13 18:31 
AnswerRe: How to add my country lunar date? PinmemberThomas Duwe7-Oct-13 8:53 
QuestionError in Namespace Pinmemberguyzfree14-Jul-13 23:31 
SuggestionRe: Error in Namespace PinmemberThomas Duwe15-Jul-13 0:18 
GeneralRe: Error in Namespace Pinmemberguyzfree15-Jul-13 1:10 
AnswerRe: Error in Namespace PinmemberThomas Duwe15-Jul-13 2:01 
GeneralRe: Error in Namespace Pinmemberguyzfree15-Jul-13 2:55 
AnswerRe: Error in Namespace PinmemberThomas Duwe16-Jul-13 22:24 
GeneralRe: Error in Namespace Pinmemberguyzfree17-Jul-13 2:39 
GeneralRe: Error in Namespace PinmemberThomas Duwe17-Jul-13 5:11 
GeneralRe: Error in Namespace Pinmemberguyzfree28-Jul-13 23:10 
GeneralRe: Error in Namespace PinmemberThomas Duwe29-Jul-13 22:32 
GeneralRe: Error in Namespace [modified] PinmemberMichealRay13-Aug-13 20:57 
Questionshowing one calendar Pinmembersh-a25-Jun-13 8:14 
AnswerRe: showing one calendar PinmemberThomas Duwe27-Jun-13 7:19 
Questionnull value Pinmembermkhalid0078-Nov-12 21:52 
AnswerRe: null value PinmemberThomas Duwe12-Nov-12 21:21 
GeneralRe: null value Pinmemberahmad_hamdan8-Dec-13 0:34 
QuestionGreat !!! PinmemberGnjus27-Oct-12 13:45 
GeneralMy vote of 5 PinmemberMember 45949287-Oct-12 9:03 
GeneralRe: My vote of 5 PinmemberThomas Duwe7-Oct-12 21:31 
BugError Custom Extend Data Picker PinmemberMember 45949287-Oct-12 8:59 
GeneralRe: Error Custom Extend Data Picker PinmemberThomas Duwe7-Oct-12 21:33 
GeneralRe: Error Custom Extend Data Picker PinmemberHernán Hegykozi8-Oct-12 4:33 
QuestionGr8 job, help PinmemberRalala Yves11-Jul-12 20:59 
AnswerRe: Gr8 job, help PinmemberThomas Duwe11-Jul-12 23:04 
QuestionMessage Removed PinmemberRalala Yves12-Jul-12 22:32 
AnswerRe: Enter the date by keyboard PinmemberThomas Duwe16-Jul-12 22:07 
GeneralMy vote of 5 Pinmemberbadboygap21-Jun-12 17:10 
GeneralRe: My vote of 5 PinmemberThomas Duwe21-Jun-12 23:28 
GeneralMy vote of 5 Pinmembersravani.v14-Jun-12 19:15 
GeneralRe: My vote of 5 PinmemberThomas Duwe14-Jun-12 22:56 

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
Web02 | 2.8.140926.1 | Last Updated 17 Jul 2012
Article Copyright 2009 by Thomas Duwe
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid