我正在处理本地化的Windows窗体应用程序。
在表单上,我已将属性“Localizable”设置为True,并且我添加了控件的本地化翻译。到目前为止一切正常。
但我的表格上还有20多个组合框,理想情况下我也希望将这些组合框的内容本地化。
我希望通过从组合框中选择其他语言,让用户可以选择在运行时更改语言。因此,我创建了两个DataTable,一个持有“语言”,一个持有组合框项目的所有变换。
这是我写的代码:
Public Class Form1
Dim myLanguages As New DataTable("cboLanguage")
Dim myDataTable As New DataTable("cboTest")
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
myLanguages.Columns.Add("Language")
myLanguages.Columns.Add("Code")
myLanguages.Rows.Add("English", "en")
myLanguages.Rows.Add("Nederlands", "nl")
myLanguages.Rows.Add("Deutsch", "de")
myLanguages.Rows.Add("Francais", "fr")
myDataTable.Columns.Add("id", System.Type.GetType("System.Int16"))
myDataTable.Columns.Add("en", System.Type.GetType("System.String"))
myDataTable.Columns.Add("nl", System.Type.GetType("System.String"))
myDataTable.Columns.Add("de", System.Type.GetType("System.String"))
myDataTable.Columns.Add("fr", System.Type.GetType("System.String"))
myDataTable.Rows.Add(1, "One", "Een", "Eins", "Un")
myDataTable.Rows.Add(2, "Two", "Twee", "Twei", "Deux")
myDataTable.Rows.Add(3, "Three", "Drie", "Drei", "Trois")
myDataTable.Rows.Add(4, "Four", "Vier", "Vier", "Quatre")
cboTest.DataSource = myDataTable
cboTest.ValueMember = "id"
'cboTest.DisplayMember is being set by the cboLanguage_SelectedIndexChanged
cboLanguage.DataSource = myLanguages
cboLanguage.ValueMember = "Code"
cboLanguage.DisplayMember = "Language"
cboLanguage.SelectedIndex = 0
End Sub
Private Sub cboLanguage_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboLanguage.SelectedIndexChanged
If cboLanguage.SelectedIndex < 0 Then Exit Sub
cboTest.DisplayMember = cboLanguage.SelectedValue.ToString
End Sub
Private Sub cboTest_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboTest.SelectedIndexChanged
If cboTest.SelectedIndex < 0 Then Exit Sub
MsgBox(cboTest.SelectedValue.ToString)
End Sub
End Class
表单有两个组合框(“cboLanguage”和“cboTest”)。
运行此代码会在表单加载时触发“cboTest_SelectedIndexChanged”两次。更改langues也会触发“cboTest_SelectedIndexChanged”,但SelectedIndex保持不变(这很好,我不希望组合框在用户更改语言时“重置”)。
这只是一个小例子,但就像我说的,我的真实形式有20多个组合框。
我应该为20多个组合框实现这个吗? 还有其他方法吗?
在处理组合框项目的本地化时是否有最佳实践?
=凸点=