开发者

"Dynamic" localized display of custom DateTimePicker format

I have a Winform application with 2 DateTimePicker's on it, initialized as follows:

private void InitializeComponent()
{
 ...
 this._StartDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
 ....
 // Note: I use a custom format, because I don't want to display seconds
 this._StartTime.CustomFormat = System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortTimePattern;
 this._StartTime.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
 ...
}

My question is: When I pick a new region from "Regional and Language Options" in the Control Panel, the _StartDate display is updated to reflect the new regional settings, but the _StartT开发者_Python百科ime is not. Why not?

UPDATE: It seems af if the System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortTimePattern is not updated until the application is restarted... But how come the Short date is displayed correctly?


The .NET framework tries to avoid big system configuration changes from destabilizing programs. This feature covers dates, times and culture info. It does so by lazily retrieving the settings and caching them so they'll always return the same value.

You can reset that cache, call CultureInfo.ClearCachedData() and TimeZoneInfo.ClearCachedData(). You could do this, for example, by writing an event handler for SystemEvents.UserPreferenceChanged. Whether you should do so is a bit questionable. It doesn't require a reboot, just restarting the app is enough.

Fwiw: the reason DateTimePicker works differently is because it is a native Windows control. It doesn't do any caching like .NET does.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜