资源文件操作类
Imports System.Resources
Public Class PublicFun
'资源文件路径
Public Shared path As String = "testResource.cn"
'资源文件访问对象
Public Shared res As New Resources.ResourceManager(path, System.Reflection.Assembly.GetExecutingAssembly())
'有key取得资源文件中的值
Public Shared Function GetStr(ByVal s As String) As String
Return res.GetString(s)
End Function
'重新加载页面
Public Shared Sub ReLoad(ByRef f As Form)
Dim baseType As String
For Each i In f.Controls
baseType = i.GetType.BaseType.ToString()
Try
'添加帅选条件
If baseType = "System.Windows.Forms.Button" Then
i.Text = PublicFun.GetStr(i.MyText)
End If
Catch e As Exception
MsgBox(e.Message)
End Try
Next
End Sub
End Class
派生自button的类
Public Class Component1
Inherits Button
Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pevent)
End Sub
Private _myText As String
'通过设置key,用取得的value初始化text属性
Public Property MyText() As String
Get
Return _myText
End Get
Set(ByVal value As String)
If value = "" Then
_myText = value
Return
End If
Dim s As String = PublicFun.GetStr(value)
If s = "" Then
MsgBox("error")
Return
Else
Text = s
_myText = value
End If
End Set
End Property
End Class
baseForm
Public Class BaseForm
Inherits Form
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
PublicFun.ReLoad(Me)
MyBase.OnLoad(e)
End Sub
End Class
Form1
Imports System.Resources
Public Class Form1
Inherits BaseForm
'通过combobox控制程序的语言
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem.ToString() = "cn" Then
PublicFun.path = "testResource.cn"
ElseIf ComboBox1.SelectedItem.ToString() = "en" Then
PublicFun.path = "testResource.en"
Else
'其他情况
Return
End If
'更新资源管理器
PublicFun.res = New ResourceManager(PublicFun.path, System.Reflection.Assembly.GetExecutingAssembly())
'重新加载页面
PublicFun.ReLoad(Me)
End Sub
Private Sub Component13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Component13.Click
'form2加载的时候自动采用最新的语言版本
Dim f As New Form2
f.Show()
End Sub
End Class