|
การทำฟอร์มหลายภาษา VB.NET
FrmMain :: หน้าจอหลัก
- Imports System.IO
- Public Class FrmMain
- Public ListViewLangItm As ListViewItem
- Public itm As ListViewItem
- Dim ListViewLangStyleIndex As Byte = 0
- Public DbListViewStyle() As SByte = {0, 1, 2, 3} ''// 0 = Large , 1 = Details , 2 = List , 3 = Tiles
- Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- 'ตั้งค่าการแสดงผลของ ListView.
- ListViewLang.MultiSelect = False
- ListViewLang.FullRowSelect = True
- ListViewLang.HideSelection = False
- 'ซ่อนปุ่มบันทึก.
- Me.BtnSave.Visible = False
- 'อ่านค่าภาษาที่บันทึกไว้ในไฟล์ตั้งค่าโปรแกรม.
- AppSelectedLanguage = XML.ReadXML(AppConfig, "GUI", "Language")
- 'กำหนดค่าแหล่งที่อยู่ของไฟล์ภาษา.
- AppLanguagePath = AppLangDir & "" & AppSelectedLanguage.TrimStart & ".xml"
- 'อ่านค่าภาษาเพื่อแสดงผล.
- Call Me.LoadLanguages()
- 'แสดงผลภาษาไปยัง Control ต่าง ๆ.
- Call ModLanguage.SetGuiLang()
- End Sub
- ''Private Sub SelecteLstViewItem(ByVal sRef As String, ByRef lvw As ListView)
- '' lvw.Focus()
- '' lvw.Select()
- '' Dim x As Integer
- '' For x = 0 To lvw.Items.Count - 1
- '' If lvw.Items(x).Text = sRef Then
- '' lvw.Items(x).Selected = True
- '' Exit For '-- if only 1 record
- '' End If
- '' Next
- ''End Sub
- Public Sub LoadLanguages()
- ''// Define Columns's Size.
- ColH.Width = 100
- ColN.Width = 150
- ColP.Width = (Me.ListViewLang.Width - (ColH.Width + ColN.Width))
- ''// Clear ListView Items.
- ListViewLang.Items.Clear()
- ''// Define ListView Properties.
- ListViewLang.HeaderStyle = ColumnHeaderStyle.Nonclickable
- ''// Set ListView Style.
- Try
- ''// 0 = Large , 1 = Details , 2 = List , 3 = Tiles
- Select Case IO.File.Exists(AppConfig)
- Case True
- ListViewLangStyleIndex = CSByte(XML.ReadXML(AppConfig, "GUI", "ListViewLang"))
- Case False
- ListViewLangStyleIndex = 3
- End Select
- ''// Apply Style.
- Select Case ListViewLangStyleIndex
- Case 0 : ListViewLang.View = View.LargeIcon
- Case 1 : ListViewLang.View = View.Details
- Case 2 : ListViewLang.View = View.List
- Case 3 : ListViewLang.View = View.Tile
- End Select
- Catch ex As Exception
- ListViewLang.View = View.Tile
- End Try
- 'ตั้งค่าการแสดงผลของ ListView.
- ListViewLang.MultiSelect = False
- ListViewLang.FullRowSelect = True
- ListViewLang.HideSelection = False
- ''// Loading Language.
- ModLanguage.LoadLang()
- ''// If not found Lang Directoy then exit.
- If Not IO.Directory.Exists(ModLanguage.AppLangDir) Then Me.BtnSave.Enabled = False : Exit Sub
- Dim supportedExtensions As String = "*.xml" '"*.xml,*.aaa,*.bbb,*.ccc,*.ddd"
- Dim files As String() = Directory.GetFiles(ModLanguage.AppLangDir, "*.*", SearchOption.TopDirectoryOnly)
- Array.Sort(files)
- ''// Search for *.xml files and show info of files.
- For Each fi As String In files
- If supportedExtensions.Contains(Path.GetExtension(fi)) Then
- Dim str(3) As String
- str(0) = " " & Path.GetFileNameWithoutExtension(fi.ToString)
- str(1) = XML.ReadXML(fi.ToString, str(0), "Language")
- str(2) = fi.ToString
- ''// Set Icon.
- Select Case str(0).TrimStart.ToUpper
- Case "EN" : itm = New ListViewItem(str, 0)
- Case "TH" : itm = New ListViewItem(str, 1)
- End Select
- ''// Add New Item with icon.
- ListViewLangItm = ListViewLang.Items.Add(itm)
- End If
- Next
- ''// Select setting value.
- Select Case ListViewLang.Items.Count
- Case Is > 0
- Me.BtnSave.Enabled = True
- For b As Integer = 0 To ListViewLang.Items.Count - 1
- If ListViewLang.Items(b).SubItems(0).Text = AppSelectedLanguage.TrimStart Then
- ListViewLang.Items(b).Selected = True
- Exit For
- End If
- Next b
- Case Else
- 'Nothing.
- End Select
- End Sub
- Private Sub ListViewLang_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListViewLang.SelectedIndexChanged
- ''// Choose Language.
- If TryCast(sender, ListView).SelectedItems.Count > 0 Then
- ''// Set App Language.
- AppSelectedLanguage = TryCast(sender, ListView).SelectedItems.Item(0).SubItems(0).Text.TrimStart
- ''// Set App Language Path.
- AppLanguagePath = TryCast(sender, ListView).SelectedItems.Item(0).SubItems(2).Text
- ''// Setup Gui Language.
- ModLanguage.SetGuiLang()
- Me.BtnSave.Visible = True
- End If
- End Sub
- Private Sub ContextIcoLargeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextIcoLargeToolStripMenuItem.Click
- Me.ListViewLang.View = View.LargeIcon
- Me.SaveListViewStyle()
- End Sub
- Private Sub ContextIcoDetailsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextIcoDetailsToolStripMenuItem.Click
- Me.ListViewLang.View = View.Details
- Me.SaveListViewStyle()
- End Sub
- Private Sub ContextIcoListToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextIcoListToolStripMenuItem.Click
- Me.ListViewLang.View = View.List
- Me.SaveListViewStyle()
- End Sub
- Private Sub ContextIcoTilesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextIcoTilesToolStripMenuItem.Click
- Me.ListViewLang.View = View.Tile
- Me.SaveListViewStyle()
- End Sub
- Public Sub SaveListViewStyle()
- ''// Save Settings.
- Dim StyleIdx As SByte = 0
- Try
- Select Case IO.File.Exists(AppConfig)
- Case True
- ''// Save Style.
- Select Case Me.ListViewLang.View
- Case View.LargeIcon : StyleIdx = DbListViewStyle(0)
- Case View.Details : StyleIdx = DbListViewStyle(1)
- Case View.List : StyleIdx = DbListViewStyle(2)
- Case View.Tile : StyleIdx = DbListViewStyle(3)
- End Select
- XML.WriteXML(AppConfig, "GUI", "ListViewLang", Format(StyleIdx, "F0"))
- Case False
- ''// 0 = Large , 1 = Details , List.
- XML.WriteXML(AppConfig, "GUI", "ListViewLang", Format(DbListViewStyle(0), "F0"))
- End Select
- Catch ex As Exception
- 'Do Not Save.
- Finally
- StyleIdx = 0
- End Try
- End Sub
- Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
- ''// Save Settings.
- Try
- Select Case IO.File.Exists(AppConfig)
- Case True : XML.WriteXML(AppConfig, "GUI", "Language", AppSelectedLanguage.TrimStart)
- Case False : XML.WriteXML(AppConfig, "GUI", "Language", AppLangSources(0))
- End Select
- MessageBox.Show("Saved.", "Language Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
- Catch ex As Exception
- MessageBox.Show(ex.Message.ToString, "Language Settings Error !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
- End Try
- Me.LoadLanguages()
- End Sub
- Private Sub MenuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuExit.Click
- Application.Exit()
- End Sub
- Private Sub MenuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuAbout.Click
- FrmAbout.ShowDialog()
- End Sub
- End Class
คัดลอกไปที่คลิปบอร์ด
Code ของฟอร์มอื่น ๆ ดูภายในไฟล์โปรเจคนะครับ
หมายเหตุ : ผมทำไว้แค่ 2 ภาษานะครับ
สาเหตุที่แยกไฟล์ภาษาออกมานอกโปรแกรมก็เพื่อให้แก้ไขข้อความภายในภาษาได้ง่ายขึ้น
และสามารถคัดลอกไฟล์ภาษา แล้วเปลี่ยนเป็นภาษาอื่น ๆ ได้อีกโดยไม่ต้องคอมไพล์โปรแกรมใหม่ (รอพัฒนาต่อครับ)
ดาวน์โหลดไฟล์
|
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน
x
|