Custom format for entire application

2019-07-16 05:23发布

问题:

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