C#如何通过一个按钮实现窗体界面的中英文切换?

 我来答
职场教育导师赵叔
2019-12-01 · TA获得超过5.1万个赞
知道小有建树答主
回答量:993
采纳率:53%
帮助的人:57.5万
展开全部

C#可以通过最基础简单的代码实现语言切换,新建一个工程,然后在视图的工具箱工具中,添加控件,通过按钮点击,实现中英文的切换。具体的方法如下:

1.新建工程:工程都是从初始窗体上一点点搭建的,本例为新建空白默认工程;

2.新建后无法往窗体上添加控件,这是由于工具箱没有打开,提供以下两种操作:快捷键:Ctrl + W,X    (即为持续按住Ctrl键后,依次按W键和X键);打开菜单栏的“视窗-工具箱”均可唤醒工具箱。取消工具箱显示,直接点击工具箱右上角“关闭”;

3.添加控件:此处用按钮、标签、窗体显示文字切换。通过点击按钮,切换中英文字。

4.添加代码:(个人选择,将待切换文字独立写入一个类,便于移植管理;也可在同一个类中写入文字定义),需定义一个语言标志,用于区分显示语言;按键点击事件中,切换语言标志,切换语言显示;

5.显示效果:运行后,点击按钮,窗体、标签、按钮均切换中英语言。

Ben
推荐于2017-10-12 · 知道合伙人软件行家
Ben
知道合伙人软件行家
采纳数:860 获赞数:2875
认真回答者.

向TA提问 私信TA
展开全部

一、窗体的国际化解决方案

新建一个WinForm解决方案后,选择主窗体,右击查看属性,找到Localizable属性,将其置为True,然后找到Language属性,选择你需要切换的语言,比如英语(美国)、中文(简体,中国)等。此时根据实际情况设计该Language下的窗体样式及语言。


 

图1 Form的属性设置


 

图2 根据选择的语言,自动生成的资源文件

二、使用CultureInfo类实现国际化解决方案

CultureInfo 类包含区域性特定的信息,例如语言、国家/地区、日历以及区域性约定。

 

 

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Windows.Forms; 

 

namespace LocationForm 

    static class Program 

    { 

        /// <summary> 

        /// 应用程序的主入口点。 

        /// </summary> 

        [STAThread] 

        static void Main() 

        { 

            Application.EnableVisualStyles(); 

            Application.SetCompatibleTextRenderingDefault(false); 

            //System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US"); 

            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN"); 

            Application.Run(new Form1()); 

        } 

    } 

三:实现效果

 


官方参考文献:http://msdn.microsoft.com/zh-cn/library/h6270d0z.aspx


另外您可能是需要动态切换语言的吧.

接着往下看.

在 Visual Studio 的设计视图中,如果在 Properties 窗口中改变了程序的默认界面语言(Language),我们会注意到无论是工程还是窗体对应的 .Designer.cs 文件都会有显著的改变。比如,我们创建一个叫 MyForm 的 form,并且添加一个叫 MyButton 的按钮。

在改变窗体 Properties 中的 Language 属性之前, .Designer.cs 代码文件中的 InitializeComponent 方法的内容大致如下:

private void InitializeComponent()

{

this.myButton = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// myButton

//

this.myButton.Location = new System.Drawing.Point(100, 200);

this.myButton.Name = "myButton";

this.myButton.Size = new System.Drawing.Size(75, 23);

this.myButton.TabIndex = 0;

this.myButton.Text = "My Button";

this.myButton.UseVisualStyleBackColor = true;

//

// myForm

//

this.ClientSize = new System.Drawing.Size(292, 273);

this.Controls.Add(this.myButton);

this.Name = "MyForm";

this.Text = "My Form";

this.ResumeLayout(false);

}


而在改变了窗体 Properties 中的 Language 属性之后,工程中除了默认的 .resx 文件之外,还会自动添加一个 .zh-CHS.resx 文件(假设我们将 Language 改变为 Chinese (Simplified))。另外,.Designer.cs 文件中的 InitializeComponent 方法也会改变成:

private void InitializeComponent()

{

System.ComponentModel.ComponentResourceManager resources

= new System.ComponentModel.ComponentResourceManager(typeof(MyForm));

this.myButton = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// myButton

//

this.myButton.AccessibleDescription = null;

this.myButton.AccessibleName = null;

resources.ApplyResources(this.myButton, "myButton");

this.myButton.BackgroundImage = null;

this.myButton.Font = null;

this.myButton.Name = "myButton";

this.myButton.UseVisualStyleBackColor = true;

//

// myForm

//

this.AccessibleDescription = null;

this.AccessibleName = null;

resources.ApplyResources(this, "$this");

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.BackgroundImage = null;

this.Controls.Add(this.myButton);

this.Font = null;

this.Icon = null;

this.Name = "myForm";

this.ResumeLayout(false);

}


我们注意到改变 Language 属性之后,代码的主要变化有:

ComponentResourceManager resources = new ComponentResourceManager(typeof(MyForm));

resources.ApplyResources(this.myButton, "myButton"); 
resources.ApplyResources(this, "$this");

另外,设置控件属性(比如显示文字 Text,控件大小 Size,显示位置 Location 等)的代码都没有了。也就是说设置控件属性的代码都是由 resources.ApplyResource 方法来完成的。那么在我们想改变 WinForm 程序的界面显示语言的时候,能不能直接调用 ApplyResources 方法呢?答案是肯定的。


为 myButton 添加 Click 事件的事件处理函数:

private void myButton_Click(object sender, EventArgs e)

{

int currentLcid = Thread.CurrentThread.CurrentUICulture.LCID;

currentLcid = (currentLcid == 2052) ? 1033 : 2052;

 

// Changes the CurrentUICulture property before changing the resources that are loaded for the win-form.

Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentLcid);

 

// Reapplies resources.

ComponentResourceManager resources = new ComponentResourceManager(typeof(MyForm));

resources.ApplyResources(myButton, "myButton");

resources.ApplyResources(this, "$this");

}

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-03-31
展开全部
重置所有显示控件的text属性 中文改为英文显示 貌似没有什么简单的方法 国际化其实也是内置多种语言 再切换的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友625e5d3
2013-03-31 · 超过10用户采纳过TA的回答
知道答主
回答量:64
采纳率:0%
帮助的人:30.3万
展开全部
把语言写成动态的放在数据库里面,供切换调用。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
帐号已注销
2013-04-05 · 超过11用户采纳过TA的回答
知道答主
回答量:30
采纳率:0%
帮助的人:28.5万
展开全部
建议你关注一下Form.Localizable / Form.Language这两个属性的用法。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式