Custom format for entire application
Asked Answered
C

1

2

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.

Counterblow answered 10/2, 2015 at 7:34 Comment(0)
P
3

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

Puke answered 10/2, 2015 at 7:38 Comment(2)
That's really good hint! i managed to do this by setting cultureInfo, but unfortunately Application.CurrentCulture.DateTimeFormat is ReadOnly so i created a new instance. It works but i already see bugs in application (a month name is not translated) and i'm afraid it will cause more problems (is there a way i can change DateTImeFormat by not creating a new instance of CultureInfo?). Second problem i faced is a grid column. my date columns are still showing date AND time until i set custom format by my own.Counterblow
I've added dateTimeInfo = (System.Globalization.DateTimeFormatInfo)Application.CurrentCulture.DateTimeFormat.Clone(); to resolve my problem with translation. Thanks!Counterblow

© 2022 - 2024 — McMap. All rights reserved.