Switch the interface language of the WinForm program at run time---------multi-language Setup Basics

Source: Internet
Author: User

System.ComponentModel.ComponentResourceManager. Applyresources Time:2015-06-17 14:59:06 read:473 comments:0 Favorites:0 [Point I collection +]

Download the code for this article:winform-multilanguages-2.rar (one-KB). Method Two:

Here's a way to make minor changes to existing code.

In the Design view of Visual Studio, if you change the program's default interface language (Language) in the Properties window, we will notice whether the project or the form corresponds. There will be significant changes to the Designer.cs file. For example, we create a form called MyForm and add a button called MyButton.

Before you change the Language property in the form properties. The contents of the InitializeComponent method in the Designer.cs code file are broadly as follows:

  • private void InitializeComponent ()
  • {
  • This.mybutton = new System.Windows.Forms.Button ();
  • This. SuspendLayout ();
  • //
  • MyButton
  • //
  • This.myButton.Location = New System.Drawing.Point (+);
  • 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);
  • }

After changing the Language property in the form properties, a. zh-chs.resx file is automatically added to the project in addition to the default. resx file (assuming we change the Language to Chinese (Simplified)). Other than that. The InitializeComponent method in the Designer.cs file will also change to:

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

We notice that after changing the Language attribute, the main changes in the code are:

    • ComponentResourceManager resources = new ComponentResourceManager (typeof (MyForm));
    • Resources.     Applyresources (This.mybutton, "MyButton"); Resources. Applyresources (This, "$this");

In addition, the code that sets the control properties (such as text text, size of the control, position location, and so on) is not present. that is, the code that sets the properties of a control is made up of resources. Applyresource method to complete the process. So when we want to change the interface display language of the WinForm program, can we call the Applyresources method directly? The answer is yes.

Add the event handler for the Click event for MyButton :

  • private void MyButton_Click (object sender, EventArgs e)
  • {
  • int currentlcid = Thread.CurrentThread.CurrentUICulture.LCID;
  • Currentlcid = (Currentlcid = = 2052)? 1033:2,052;
  • Changes the CurrentUICulture property before changing the resources is 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");
  • }

When the program runs, click the MyButton button on the form, and the interface display language will switch between English and Simplified Chinese.

Switches the interface language System.ComponentModel.ComponentResourceManager of the WinForm program at run time. Applyresources

Tags: des WinForm class style COM code http SI it

Original: http://www.cnblogs.com/1175429393wljblog/p/4583198.html

Switch the interface language of the WinForm program at run time---------multi-language Setup Basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.