Muhammet Yavuz
Muhammet Yavuz

Reputation: 41

Change DateTimePicker Calendar Runtime

I want to view the calendar in another culture, for example let it show in Arabic. I use this code in the Main, but the calendar is still in the same culture:

CultureInfo sa = new CultureInfo("ar-SA", false);
sa.DateTimeFormat.Calendar = new System.Globalization.HijriCalendar();
// Sets the culture to Arabic (Saudi Arabia)
Thread.CurrentThread.CurrentCulture = sa;
// Sets the UI culture to Arabic (Saudi Arabia)
Thread.CurrentThread.CurrentUICulture = sa;

I add the DateTimePicker on a form and expect that it shows the date in Arabic after running, but the DateTimePicker or Calendar is not changed to that culture.

Upvotes: 2

Views: 6307

Answers (3)

A Ghazal
A Ghazal

Reputation: 2823

You can change the datepicker culture and format at runtime. For example the following changes the format to en-US:

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
    dateTimePicker1.Format = DateTimePickerFormat.Custom;
    dateTimePicker1.CustomFormat = Application.CurrentCulture.DateTimeFormat.ShortDatePattern;

On the other hand , use this to changes the culture and the date format of the datepicker to Saudi Arabia:

    Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-SA");
    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-SA");
    dateTimePicker1.Format = DateTimePickerFormat.Custom;
    dateTimePicker1.CustomFormat = Application.CurrentCulture.DateTimeFormat.ShortDatePattern;

Upvotes: 2

Muhammet Yavuz
Muhammet Yavuz

Reputation: 41

The DateTimePicker and MonthCalendar control do not reflect the CurrentUICulture property of an application's main execution thread when you created a localized application in the .NET Framework, in Visual Studio 2005, or in Visual Studio .NET

Upvotes: 2

Alex
Alex

Reputation: 8116

This changes the displayed format of the DateTimePicker - not date in the expanded view though.

  CultureInfo ci = new CultureInfo("ar-SA");
  DateTimeFormatInfo info = ci.DateTimeFormat;
  dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
  dateTimePicker1.CustomFormat = info.ShortDatePattern + " " + info.ShortTimePattern;

Upvotes: 0

Related Questions