如何为整个 winform 应用程序设置文化 [英] How set a culture for entire winform application

查看:25
本文介绍了如何为整个 winform 应用程序设置文化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为整个 winform 应用程序设置文化.
我该怎么做?
我像这样改变了我的 Program.cs 文件:

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

我做对了吗?
<小时>还有一个关于那个的链接:(请看一看)
https://www.c-sharpcorner.com/forums/set-cultureinfo-for-winform-application

这取得了有限的成功,因此在我放置的 InitializeComponent() 之前的Form1"顶部:

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");

是否有必要在每个表单的 InitializeComponent() 之前添加这三行?

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

推荐答案

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

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

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

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

这篇关于如何为整个 winform 应用程序设置文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆