如何为整个Winform应用程序树立文化

时间:2018-07-30 22:09:59

标签: c# winforms culture .net-4.6.2

我想为整个winform应用程序树立文化。
我该怎么办?
我这样更改了Program.cs文件:

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

我做对了吗?


另外还有一个有关的链接:(请看看)
https://www.c-sharpcorner.com/forums/set-cultureinfo-for-winform-application

这是有限的成功,因此在我放置的InitializeComponent()之前的“ Form1”顶部:

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

是否有必要在每种形式的InitializeComponent()之前添加这三行?

1 个答案:

答案 0 :(得分:1)

Main中将这两个设置为所需的区域性: CultureInfo.DefaultThreadCurrentCulture CultureInfo.DefaultThreadCurrentUICulture

此外,只要您想更改应用程序当前线程上的区域性,就可以更改Application.CurrentCulture

[STAThread]
static void Main()
{
    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());
}