问题描述
如何为整个应用程序设置/强制使用通用格式?
How to set/enforce common format for entire application?
对于像dateTimePicker
这样的网格和控件,只是在开发时不担心它?
For grids and controls like dateTimePicker
just to not worry about it while developing?
我试图在控件上设置自己的包装,但是每次运行设计器时,该值都会被覆盖.恐怕当我要更改它时,我必须到处更改它!令人毛骨悚然;)
I tried to set in my own wrapper on controls but every time I run the designer this value is overwritten. I'm afraid that when I want to change it I'd have to change it everywhere! that's creepy ;)
另一种想法是将其设置在
Another thought was to set it in .cs
file in
SetInitValues( customformat = Format+From_Config_file_or_settings;)
还是什么..?
但是在这种情况下,我必须在每个表单上都添加此方法.
But in this case I have to add this method on every single form.
还有其他更好的方法吗?
It is any other - better - way to do this?
基于以下我创建的答案:
Based on the answer below i've created:
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
...
SetCustomFormatting();
...
}
private static void SetCustomFormatting()
{
// Application.CurrentCulture.DateTimeFormat is ReadOnly.
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("pl-PL");
System.Globalization.DateTimeFormatInfo dateTimeInfo = new System.Globalization.DateTimeFormatInfo();
// without this all strings like month names are not translated!
dateTimeInfo = (System.Globalization.DateTimeFormatInfo)Application.CurrentCulture.DateTimeFormat.Clone();
dateTimeInfo.DateSeparator = ".";
dateTimeInfo.LongDatePattern = "dddd, dd MMMM yyyy";
dateTimeInfo.ShortDatePattern = "yy.MM.dd";
dateTimeInfo.LongTimePattern = "hh:mm:ss tt";
dateTimeInfo.ShortTimePattern = "hh:mm tt";
dateTimeInfo.FullDateTimePattern = "yyyy.MM.dd HH:mm:ss";
cultureInfo.DateTimeFormat = dateTimeInfo;
Application.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
}
}
这是个好方法吗?
非常感谢您的建议.
推荐答案
您可以使用 Culture 属性设置格式和其他相关设置,因此您无需手动调整所有内容.
You can set the formats and other dependant settings with the Culture property, so you don't have to tweak everything by hand.
Here's a start to look at: https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.defaultthreadcurrentculture%28v=vs.110%29.aspx
这篇关于整个应用程序的自定义格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!