代码之家  ›  专栏  ›  技术社区  ›  SilverLight

如何为整个winform应用程序设置区域性

  •  0
  • SilverLight  · 技术社区  · 6 年前

    我想为整个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 回复  |  直到 6 年前
        1
  •  2
  •   Bizhan    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());
    }