Switches the interface language System.ComponentModel.ComponentResourceManager of the WinForm program at run Time. Applyresources
Source: Internet
Author: User
<span id="Label3"></p>Download the code for this Article:winform-multilanguages-2.rar (one-KB). method two:<p><p>Here's a way to make minor changes to existing Code.</p></p><p><p>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.</p></p><p><p>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:</p></p> <ul class="prelist"> <li><li>private void InitializeComponent ()</li></li> <li><li>{</li></li> <li><li><span class="indent1">This.mybutton = new System.Windows.Forms.Button ();</span></li></li> <li><li><span class="indent1">This. SuspendLayout ();</span></li></li> <li><li><span class="indent1"><span class="codecomment">//</span></span></li></li> <li><li><span class="indent1"><span class="codecomment">MyButton</span></span></li></li> <li><li><span class="indent1"><span class="codecomment">//</span></span></li></li> <li><li><span class="indent1">this.myButton.Location = new System.Drawing.Point (100, 200);</span></li></li> <li><li><span class="indent1">This.myButton.Name = "myButton";</span></li></li> <li><li><span class="indent1">this.myButton.Size = new System.Drawing.Size (75, 23);</span></li></li> <li><li><span class="indent1">This.myButton.TabIndex = 0;</span></li></li> <li><li><span class="indent1">This.myButton.Text = "My button";</span></li></li> <li><li><span class="indent1">This.myButton.UseVisualStyleBackColor = true;</span></li></li> <li><li><span class="indent1"><span class="codecomment">//</span></span></li></li> <li><li><span class="indent1"><span class="codecomment">MyForm</span></span></li></li> <li><li><span class="indent1"><span class="codecomment">//</span></span></li></li> <li><li><span class="indent1">This. ClientSize = new System.Drawing.Size (292, 273);</span></li></li> <li><li><span class="indent1">This. Controls.Add (this.mybutton);</span></li></li> <li><li><span class="indent1">This. Name = "MyForm";</span></li></li> <li><li><span class="indent1">This. Text = "My Form";</span></li></li> <li><li><span class="indent1">This. ResumeLayout (false);</span></li></li> <li><li>}</li></li> </ul><p><p></p></p><p><p>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:</p></p> <ul class="prelist"> <li><li>private void InitializeComponent ()</li></li> <li><li>{</li></li> <li><li><span class="indent1"><strong>System.ComponentModel.ComponentResourceManager Resources</strong></span></li></li> <li><li><span class="indent2"><strong>= new System.ComponentModel.ComponentResourceManager (typeof (MyForm));</strong></span></li></li> <li><li><span class="indent1">This.mybutton = new System.Windows.Forms.Button ();</span></li></li> <li><li><span class="indent1">This. SuspendLayout ();</span></li></li> <li><li><span class="indent1"><span class="codecomment">//</span></span></li></li> <li><li><span class="indent1"><span class="codecomment">MyButton</span></span></li></li> <li><li><span class="indent1"><span class="codecomment">//</span></span></li></li> <li><li><span class="indent1">This.myButton.AccessibleDescription = null;</span></li></li> <li><li><span class="indent1">This.myButton.AccessibleName = null;</span></li></li> <li><li><span class="indent1"><strong>Resources. Applyresources (this.mybutton, "myButton");</strong></span></li></li> <li><li><span class="indent1">This.myButton.BackgroundImage = null;</span></li></li> <li><li><span class="indent1">This.myButton.Font = null;</span></li></li> <li><li><span class="indent1">This.myButton.Name = "myButton";</span></li></li> <li><li><span class="indent1">This.myButton.UseVisualStyleBackColor = true;</span></li></li> <li><li><span class="indent1"><span class="codecomment">//</span></span></li></li> <li><li><span class="indent1"><span class="codecomment">MyForm</span></span></li></li> <li><li><span class="indent1"><span class="codecomment">//</span></span></li></li> <li><li><span class="indent1">This. AccessibleDescription = null;</span></li></li> <li><li><span class="indent1">This. AccessibleName = null;</span></li></li> <li><li><span class="indent1"><strong>Resources. Applyresources (this, "$this");</strong></span></li></li> <li><li><span class="indent1">This. AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;</span></li></li> <li><li><span class="indent1">This. BackgroundImage = null;</span></li></li> <li><li><span class="indent1">This. Controls.Add (this.mybutton);</span></li></li> <li><li><span class="indent1">This. Font = null;</span></li></li> <li><li><span class="indent1">This. Icon = null;</span></li></li> <li><li><span class="indent1">This. Name = "myForm";</span></li></li> <li><li><span class="indent1">This. ResumeLayout (false);</span></li></li> <li><li>}</li></li> </ul><p><p></p></p><p><p>We notice that after changing the Language attribute, the main changes in the code are:</p></p> <ul class="prelist"> <ul class="prelist"> <li>ComponentResourceManager resources = new ComponentResourceManager (typeof (MyForm));</li> <li>Resources. Applyresources (this.mybutton, "myButton"); Resources. Applyresources (this, "$this");</li> </ul> </ul><p><p>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. <span class="term">that is, the code that sets the properties of a control is made up of <strong>Resources. Applyresource</strong> 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. </span></p></p><p><p></p></p><p><p>Add the event handler for the Click event for MyButton:</p></p> <ul class="prelist"> <ul class="prelist"> <li> private void MyButton_Click (object sender, EventArgs e) </li> <li> {</li> <li> <span class="inden T1 ">int currentlcid = Thread.CurrentThread.CurrentUICulture.LCID; </span> </li> <li> <span class="indent1">currentlcid = (currentlcid = = 2052)? 1033:2052; </span> </li> <li> </li> <li> <span class="indent1"><span class="codecomment">//changes the CurrentUICulture property before Changing the resources that is loaded for the win-form. </span> </span> </li> <li> <span class="indent1">thread.currentthread.currentuiculture = new CultureInfo (currentlcid); </span> </li> <li> </li> <li> <span class="indent1"><span class="codecomment">//reapplies resources. </span> </span> </li> <li> <span class="indent1">componentresourcemanager resources = new ComponentResourceManager (typeof (MyForm)); </span> </li> <li> <span class="indent1">resources. Applyresources (myButton, "myButton"); </span> </li> <li> <span class="indent1">resources. Applyresources (this, "$this"); </span> </li> <li>} </li> </ul> </ul><p><p></p></p><p><p>When the program runs, click the MyButton button on the form, and the interface display language will switch between English and simplified Chinese.</p></p><p><p>Switches the interface language System.ComponentModel.ComponentResourceManager of the WinForm program at run Time. Applyresources</p></p></span>
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