windows 更改 DateTimePicker 日历运行时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8141437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change DateTimePicker Calendar Runtime
提问by Mohamad Draow
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:
我想查看另一种文化中的日历,例如让它以阿拉伯语显示。我在 Main 中使用此代码,但日历仍处于相同的文化中:
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.
我在表单上添加 DateTimePicker 并期望它在运行后以阿拉伯语显示日期,但 DateTimePicker 或 Calendar 不会更改为该文化。
采纳答案by Mohamad Draow
The DateTimePicker and MonthCalendarcontrol 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
在 .NET Framework、Visual Studio 2005 或 Visual Studio .NET 中创建本地化应用程序时,DateTimePicker 和 MonthCalendar控件不反映应用程序主执行线程的 CurrentUICulture 属性
回答by A Ghazal
You can change the datepicker culture and format at runtime. For example the following changes the format to en-US:
您可以在运行时更改日期选择器文化和格式。例如,以下将格式更改为 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;
回答by Alex
This changes the displayed format of the DateTimePicker - not date in the expanded view though.
这会更改 DateTimePicker 的显示格式 - 虽然不是展开视图中的日期。
CultureInfo ci = new CultureInfo("ar-SA");
DateTimeFormatInfo info = ci.DateTimeFormat;
dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = info.ShortDatePattern + " " + info.ShortTimePattern;