How set a culture for entire winform application

折月煮酒 提交于 2020-08-09 08:17:54

问题


I want to set a culture for entire winform application.
How can i do that?
I changed my Program.cs file like this :

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Divar
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            var culture = new CultureInfo("en-US");
            CultureInfo.DefaultThreadCurrentCulture = culture;
            CultureInfo.DefaultThreadCurrentUICulture = culture;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new RadForm1());
        }
    }
}

Did i do it right?


Also there is another link about that :(Please take a look)
https://www.c-sharpcorner.com/forums/set-cultureinfo-for-winform-application

This has limited success, so at the top of "Form1" before the InitializeComponent() I placed:

    System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("en-GB");
    Application.CurrentCulture = cultureInfo;
    System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-GB");

Is it necessary to add those three lines before the InitializeComponent() in every form?


回答1:


Set these two in Main to the desired culture: CultureInfo.DefaultThreadCurrentCulture CultureInfo.DefaultThreadCurrentUICulture

Additionally, you can change Application.CurrentCulture whenever you want to change the culture on the current thread of the application.

[STAThread]
static void Main()
{
    var culture = CultureInfo.GetCultureInfo("en-US");

    // this may fail sometimes: (see Drachenkatze's comment below)
    // var culture = new CultureInfo("en-US");

    //Culture for any thread
    CultureInfo.DefaultThreadCurrentCulture = culture;

    //Culture for UI in any thread
    CultureInfo.DefaultThreadCurrentUICulture = culture;

    //Culture for current thread (STA)
    //no need for: Application.CurrentCulture = culture;

    //Thread.CurrentThread.CurrentCulture == Application.CurrentCulture
    //no need for: Thread.CurrentThread.CurrentCulture = culture;

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new RadForm1());
}


来源:https://stackoverflow.com/questions/51602944/how-set-a-culture-for-entire-winform-application

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