如何为整个 winform 应用程序设置文化

Sil*_*ght 2 c# culture winforms .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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我做对了吗?


还有另一个链接:(请看一看)
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");
Run Code Online (Sandbox Code Playgroud)

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

Biz*_*han 5

将这两个设置Main为所需的文化: CultureInfo.DefaultThreadCurrentCulture CultureInfo.DefaultThreadCurrentUICulture

此外,您可以随时更改Application.CurrentCulture应用程序当前线程上的文化。

[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());
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,`new CultureInfo("en-US");` 没有检索到 `en-US` (.NET 4.7.2) 的 `CultureInfo`。文化仍然有德国小数点分隔符。我不得不使用 `CultureInfo.GetCultureInfo("en-US");` 来让它工作。 (3认同)