C#中英文切换

在C#项目软件开发中,经常会涉及到窗体界面语言的选择和设置,便于软件能够国际化。今天自己动手实践了一下。

1、新建Windows窗体应用程序,在窗体中放置两个label1和label2,分别命名为中文,英文;

2、在窗体中放置两个RadioButton按钮,分别命名为radioButton1和radioButton2;

3、当选中“中文”(radioButton1)时,将其属性Checked设置为True,

同时将窗体属性中的“Language”设为”中文(简体)“将”Localizable”属性设置为“True”,

然后,双击窗体中的radioButton按钮跳转到该事件的方法,并写入如下代码:

        private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture =
               new System.Globalization.CultureInfo(“zh-CN”);
            ApplyResource();
        }

4、同3所示,选中“英文”(radioButton)时,将其属性Checked设置为”True“,

同时将窗体属性中的“Language”设为”英文(美国)“将”Localizable”属性设置为“True”,

然后,将窗体中的相应的设置按钮,比如Lable、Button等的名称命名为英语,

最后补充事件(radioButton2)代码,如下所示:

        private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture =
                new System.Globalization.CultureInfo(“en-US”);
            ApplyResource();
        }

5、对于ApplyResource()方法其作用是实现对当前语言界面的动态加载,实现如下所示:

        private void ApplyResource()
        {
            foreach (Control ctl in this.Controls)
            {
                resource.ApplyResources(ctl, ctl.Name);
            }
            this.ResumeLayout(false);
            this.PerformLayout();
            resource.ApplyResources(this, “$this”);
        }

6、对于上述操作实现的结构如

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2017年5月6日
下一篇 2017年5月6日

相关推荐