65.9K
CodeProject is changing. Read more.
Home

Multi Culture Calendar

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.43/5 (13 votes)

Apr 26, 2008

CPOL
viewsIcon

47362

downloadIcon

1446

A calendar which supports all types of calendars such as persian(Farsi)-Arabic and English calendars by changing the culture

Introduction

As you know, after changing the culture, all controls work with the new culture. In this control, you can use several calendars on a page and change the culture for each one.

The important thing is that Microsoft does not support the UI of the Persian calendar. Now you can use it very easily. It is possible to inherit this control from other controls which you want and make your custom controls.

Using the Code

The code is very easy and I override just the render method. Before the rendering and after that, I change the culture and only for Persian, I use Reflection to change the calendar (Shamsi Calendar).

Here is the important function on this control that shows you how it can be made possible using the Persian calendar:

private void ChangeCulture()
{
DefaultCulture = new System.Globalization.CultureInfo(
    System.Threading.Thread.CurrentThread.CurrentUICulture.Name);

switch (SelectedCulture.Name)
{
case "fa-IR":
System.Globalization.DateTimeFormatInfo info = SelectedCulture.DateTimeFormat;
info.AbbreviatedDayNames = new string[] { "ی", "§", "«", "چ", "پ", "¤", "¬" };
info.DayNames = new string[] { "یک¬ë ى", "§ي¬ë ى", "ﺳﻪ¬ë ى", "چىں©¬ë ى", "پ뤬ë ى",
    "¤êمى", "¬ë ى" };
info.AbbreviatedMonthNames = new string[] { "ه©ي©§یë", "ں©§ï ى¬¢", "¦©§ں§", "¢ï©",
    "ê©§ں§", "¬ى©یي©", "êى©", "™ ںë", "™¨©", "§ی", " ىêë", "ں«هë§", "" };
info.MonthNames = new string[] 
    { "ه©ي©§یë", "ں©§ï ى¬¢", "¦©§ں§", "¢ï©", "ê©§ں§", "¬ى©یي©", 
    "êى©", "™ ںë", "™¨©", "§ی", " ىêë", "ں«هë§", "" };
info.AMDesignator = "ç.â";
info.PMDesignator = " .â";
info.ShortDatePattern = "yyyy/MM/dd";
info.FirstDayOfWeek = DayOfWeek.Saturday;
System.Globalization.PersianCalendar cal = new System.Globalization.PersianCalendar();
typeof(System.Globalization.DateTimeFormatInfo).GetField(
    "calendar", System.Reflection.BindingFlags.Public |
    System.Reflection.BindingFlags.Instance |
    System.Reflection.BindingFlags.NonPublic).SetValue(info, cal);

object obj = typeof(System.Globalization.DateTimeFormatInfo).GetField(
    "m_cultureTableRecord", System.Reflection.BindingFlags.Public |
    System.Reflection.BindingFlags.Instance |
    System.Reflection.BindingFlags.NonPublic).GetValue(info);

obj.GetType().GetMethod("UseCurrentCalendar",
   System.Reflection.BindingFlags.NonPublic |
   System.Reflection.BindingFlags.Instance).Invoke(obj,
   new object[] { cal.GetType().GetProperty("ID",
   System.Reflection.BindingFlags.Instance |
   System.Reflection.BindingFlags.NonPublic).GetValue(cal, null) });

typeof(System.Globalization.CultureInfo).GetField("calendar",
    System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance |
    System.Reflection.BindingFlags.NonPublic).SetValue(SelectedCulture, cal);

System.Threading.Thread.CurrentThread.CurrentCulture = SelectedCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = SelectedCulture;
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat = info;
System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat = info;
break;

default:
System.Threading.Thread.CurrentThread.CurrentCulture = SelectedCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = SelectedCulture;
break;
}
}