Step 1.
開啟VB.NET -> 檔案 -> 新增專案 -> 選擇Visual Basic -> 選擇Windows Form 應用程式 -> 按下確定
Step 2.
專案內新增兩個Form(Form1 與 Form2)
Setp 3.
點選Form1 -> 顯示屬性 -> 找到Language欄位 -> 選擇英文
Form2也一樣如法炮製
Step 4.
接著進行編輯Form1與Form2內,各元件屬性的text欄位
此時 方案總管點選上方的"顯示所有檔案"圖示,並展開Form1.vb與Form2.vb
會發現各多了一個.en-US.resx的檔案
Step 5.
點開.resx後會看到剛剛對各元件編輯的列表
點選Form1 -> 顯示屬性 -> 找到Language欄位 -> 選擇中文 -> 重複Step 4 ~ 5
將Form1中用來更換語言的Button,其屬性的Name改為Btn_Main,此Button也為觸發主要按鈕
Step 9.
使用方式主要為修改與更新
語法使用如下:
設定要顯示的語言
System.Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo("en-US")
更新表格元件內容
_C_UILanguage._Main(Me)_C_UILanguage._Main(Form2)
其中紅色字樣部分的地方,可參考方案總管內,各.resx檔名來填入,以本文為例,可填en-US 或者 zh-TW
而更新表格元件,已被我包到Class裡面,可以直接使用,如想更了解,下列貼上完整的程式碼,可供參考!!
執行結果如下
完整程式碼範例:
類別
Imports System Imports System.ComponentModel Imports System.Windows.Forms Public Class C_UILanguage Dim CRM As System.ComponentModel.ComponentResourceManager Public Sub _Main(ByVal _Form As Windows.Forms.Form) '檢查引數 If _Form Is Nothing Then _Form.Text = "Form" Throw New ArgumentNullException(_Form.Text) End If CRM = Nothing '切換介面顯示的語言 Try CRM = New ComponentResourceManager(_Form.GetType) For Each Control As Control In _Form.Controls Control.Text = "Control" _Control_Resource(Control, CRM) Next Finally CRM = Nothing End Try End Sub Private Sub _Control_Resource(ByRef _Control As Control, ByRef _CRM As ComponentResourceManager) '檢查引數 If _Control Is Nothing Then Throw New ArgumentNullException("Control") End If If _CRM Is Nothing Then Throw New ArgumentNullException("CRM") End If _CRM.ApplyResources(_Control, _Control.Name) If TypeOf _Control Is MenuStrip Then Dim MS As MenuStrip = CType(_Control, System.Windows.Forms.MenuStrip) For Each TSI As ToolStripItem In MS.Items _Strip_Resource(TSI, _CRM) Next ElseIf TypeOf _Control Is ToolStrip Then Dim TS As ToolStrip = CType(_Control, ToolStrip) For Each TSI As ToolStripItem In TS.Items _Strip_Resource(TSI, _CRM) Next ElseIf _Control.Controls.Count > 0 Then For Each Control As Control In _Control.Controls _Control_Resource(Control, _CRM) Next End If End Sub Private Sub _Strip_Resource(ByRef _TSI As ToolStripItem, ByRef _CRM As ComponentResourceManager) '檢查引數 If _TSI Is Nothing Then Throw New ArgumentNullException("TSI") End If If _CRM Is Nothing Then Throw New ArgumentNullException("CRM") End If _CRM.ApplyResources(_TSI, _TSI.Name) If TypeOf _TSI Is ToolStripMenuItem Then Dim TSM As ToolStripMenuItem = CType(_TSI, ToolStripMenuItem) For Each TS As ToolStripItem In TSM.DropDownItems _Strip_Resource(TS, _CRM) Next End If End Sub End Class
主程式
Public Class Form1 Dim _C_UILanguage As New C_UILanguage Private Sub Btn_Main_Click(sender As Object, e As EventArgs) Handles Btn_Main.Click If Btn_Main.Text = "English" Then System.Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo("zh-TW") _C_UILanguage._Main(Me) _C_UILanguage._Main(Form2) Btn_Main.Text = "中文" ElseIf Btn_Main.Text = "中文" Then System.Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo("en-US") _C_UILanguage._Main(Me) _C_UILanguage._Main(Form2) Btn_Main.Text = "English" End If End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Form2.Show() End Sub End Class
沒有留言:
張貼留言