Custom format for entire application

若如初见. 提交于 2020-01-15 06:42:07

问题


How to set/enforce common format for entire application?

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;) 

or something..?

But in this case I have to add this method on every single form.

It is any other - better - way to do this?

EDIT: 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;
            }
        }

Is it a good way to do this?

thanks a lot for advice.


回答1:


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



来源:https://stackoverflow.com/questions/28426349/custom-format-for-entire-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!