【发布时间】:2012-05-28 07:44:21
【问题描述】:
据this article我知道,那个DateTimePicker控件不反映CurrentUICulture
那么,我怎样才能获得它所使用的真实文化?我需要在多语言应用程序中获取 DateTimePicker 的字符串值,但 完全 格式与用户在表单上看到的格式相同(不,我无法设置“硬编码”自定义格式)。
示例:
当我使用本地波兰语设置在 Windows 上工作时,对于 LongTime,它是:HH:mm:ss 并且 DateTimePicker 中的时间显示为:09:26:50
现在,当我想在我的应用程序中选择不同的语言并在表单的构造函数中将文化设置更改为 US 时:
...
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
InitializeComponent();
....
然后
MessageBox.Show(System.Globalization.DateTimeFormatInfo.CurrentInfo.LongTimePattern);
显示,新的时间格式为:h:mm:ss tt,但 DateTimePicker 仍在显示:09:26:50
当我尝试获取时间字符串时:
dateTimePicker1.Value.ToLongTimeString()
或
dateTimePicker1.Value.ToString(System.Globalization.DateTimeFormatInfo.CurrentInfo.LongTimePattern)
然后时间显示为:9:26:50 AM
所以我的问题是 - 我怎么知道 DateTimePicker 仍在使用系统的 CultureInfo("pl-PL") 或者它的显示格式是:HH:mm:ss ?
【问题讨论】:
-
尝试将设置文化的代码移动到 InitializeCulture (msdn.microsoft.com/en-us/library/…)。你有没有在有时间之前检查过你的文化?
标签: c# format globalization datetimepicker