Hiển thị các Label bằng chữ ViệtBài nầy đi theo sau bài Một Editor đơn giản cho Unicode , trong đó có chỉ cách dùng VB6 trong việc lập trình một Editor đơn giản cho Unicode. Program nầy bước thêm một bước nữa là gói các chức năng trong việc bỏ dấu vào một Class tên clsUnicodeEditor để ta có thể dùng nó một cách tổng quát chung với các MSForm2 controls hổ trợ Unicode tiếng Việt.
Ghi chú:Trước hết muốn hiển thị Unicode cho chữ Việt ta cần phải dùng Menu command của VB6 IDE để Project | Components Microsoft Forms 2.0 Object Library. Cái ActiveX nầy cho ta những Label, TextBox, Listbox và ComboBox cần thiết để hiển thị chữ Việt trong Unicode.
Kế đó, để đọc và viết chữ Việt dưới dạng UTF-8 Unicode, ta chứa Unicode text file trong một XML file giữa một cặp tags tên Text hay tên gì cũng được (đó cũng là root node) , rồi dùng Microsoft Document Object Model (DOM) để đọc và viết chữ Việt. Bạn nhớ Project | References Microsoft XML, v3.0. Nguyên phần Text là nodeTypedValue của root node của DOM.
Làm như thế ta tránh phải đọc từng byte rồi tìm cách chuyển data ấy qua Unicode String. Ở đây xin nhấn mạnh là bạn phải vui lòng dùng MSWindowsNT hay MSWindows2000 mới được. Bạn có thể dùng Notepad trong WindowsNTđể edit XML file chứa chữ Việt và lưu trử dưới format UTF-8. Nghe nói Windows98SE hay Windows Millenium cũng đuợc, dĩ nhiên WindowsXP thì OK.
Ngoài ra để hiển thị record data chữ Việt Unicode trong một Grid bạn cũng cần phải Project | Components Microsoft Hierarchical FlexGrid Control 6.0 (OLEDB).Trong program nầy ta cho user edit các record của một table tên Staff trong một Access 2000 database tên employee.mdb. Khi khởi động, program sẽ hiển thị data của record thứ nhất trong các MSForm2 Textboxes. Ðồng thời, data của các record cũng được hiển thị trong một MSHFlexGrid. Ở đây ta dùng MSHFlexGrid vì nó hổ trợ Unicode cho chữ Việt. Sau đó bạn có thể di chuyển từ record nầy đến record khác bằng cách bấm các nút Navigator như |<, <, >, >| để di chuyển đến record thứ nhất, record trước đó, record sau đó và record cuối cùng, hay bấm lên một row của Grid. Mỗi lần như thế thì các Textboxes sẽ được updated với data của newly selected (hay current) record. Bên trái của current record trên Grid luôn luôn có hiển thị character > để đánh dấu.
Nếu bạn muốn Edit một record nào thì select nó rồi bấm nút Sửa đổi. Các Textboxes sẽ đuợc unlocked (mở khóa) cho bạn Edit bằng chữ Việt. Nếu Edit một record có sẵn, bạn không thể Edit FullName.
Bạn có thể chọn đánh máy bỏ dấu theo lối VNI (tức là dùng các con số 1,2,3,4,5,6,7,8,9 và d ) hay theo lối VIQR (tức là dùng ',`,?,~,.,^,+ hay *, (, và d hay -).
Mục đích của program nầy không phải để chỉ bạn cách dùng Access 2000 trong VB6 nhưng để biểu diễn các cách lập trình với Unicode trong VB6 để giúp bạn khỏi mất nhiều thì giờ nghiên cứu. Do đó nếu bạn thấy program có bug khi Edit hay hiển thị các records thì bạn đừng ngạc nhiên.

Set MyUnicodeText = New clsUnicodeText ' Read the list of English=Viet captions VLabels = MyUnicodeText.ReadUnicode(GetLocalDirectory & "Labels.xml") ' Convert English captions of the Label1s to Vietnamese For i = 0 To 2 Label1(i).Caption = VietCaptionOf(Label1(i).Caption) Label1(i).Font.Name = "Tahoma" ' Assign Font Tahoma to it Next
Cái Sub
VietCaptionOf dùng trong việc nầy có listing như dưới đây:
Class UnicodeEditor
Function VietCaptionOf(OrigCaption) As String ' Map an English caption to its corresponding Viet Unicode string Dim StartPos, EndPos ' Append the "=" character OrigCaption = OrigCaption & "=" ' Locate the substring in VLabels StartPos = InStr(VLabels, OrigCaption) If StartPos = 0 Then ' Not found - use the caption as is VietCaptionOf = OrigCaption Else ' Found - extract the corresponding Viet string StartPos = StartPos + Len(OrigCaption) EndPos = InStr(StartPos, VLabels, "|") ' Viet string ends with "|" ' Return the Viet string VietCaptionOf = Mid(VLabels, StartPos, EndPos - StartPos) End If End Function
Sub UnicodeTextToListBox(ByVal Utext, LV) Dim Pos ' Split up into lines to load the Listbox LV Pos = InStr(Utext, vbLf) ' Locate Line Feed character Do While Pos > 0 ' Pluck a line from the left of the UText string and add it to LV LV.AddItem Left(Utext, Pos - 1) ' Keep the remaining Utext = Mid(Utext, Pos + 1) ' Locate the next Line Feed character Pos = InStr(Utext, vbLf) Loop LV.AddItem Utext ' Add the last piece of text to Listbox LV End Sub
' Class clsUnicodeEditor Private Sub Class_Initialize() ' Instantiate an invisible working form to use its Listbox and Textbox Set myForm = New frmWorking ' Point local Listbox pointer LV to the Form's Listbox Set LV = myForm.ListBox1 ' Point local TextBox pointer LastCh to the Form's Textbox Set LastCh = myForm.TextBox1 ' Instantiate an object of Class clsUnicodeText to read Unicode Text Set MyUnicodeText = New clsUnicodeText ' Read the list of Unicode Vowels UVowels = MyUnicodeText.ReadUnicode(GetLocalDirectory & "UnicodeVowels.xml") ' Read the look-up table for Vowels with squiggles Vowels = MyUnicodeText.ReadUnicode(GetLocalDirectory & "V1.xml") ' Split up into lines to load the Listbox LV UnicodeTextToListBox Vowels, LV mTypingStyle = "VNI" ' use VNI typing style initially End Sub
Trong Sub
Form_Load của frmEmployees ta Instantiate một object của class
clsUnicodeEditor rồi attach nó vào cái Textbox cần hổ trợ Việt Unicode
typing như sau:
' Instantiate an object of class clsUnicodeEditor Set TField1 = New clsUnicodeEditor TField1.Init TextBox1 ' Attache it to TextBox1 ' Instantiate an object of class clsUnicodeEditor Set TField2 = New clsUnicodeEditor TField2.Init TextBox2 ' Attache it to TextBox2 ' Instantiate an object of class clsUnicodeEditor Set TField3 = New clsUnicodeEditor TField3.Init TextBox3 ' Attache it to TextBox3
Program có hổ trợ Escape character \. Con số theo sau Escape character \ sẽ không bị dùng vào việc bỏ dấu cho nguyên âm ngay trước đó. Ngoài ra thí dụ User đánh theo lối VNI letter a và 1, ta sẽ đuợc chữ á, nếu tiếp theo đó User đánh 1 một lần nữa ta sẽ đuợc a1.
Xin lưu ý: Ðể program nầy chạy bỏ dấu đuợc bạn phải tạm thời ngưng các programs như VPSKEYS hay VietKey, UniKey .v.v.. Lý do là các programs kia sẽ giựt trước các keystrokes của những con số 1 đến 9, sau khi kiểm tra rồi không chịu buông ra cho program nầy thấy.
Bạn có thể download source code của program mẫu nầy kể cả class clsUnicodeText và class clsUnicodeEditor. Program nầy không có dùng ADOdc làm Datasource để hiển thị trực tiếp (data binding) từ database record của Access 2000 lên TextBoxes hay MSHFlexgrid, nhưng bạn có thể làm đuợc việc ấy.
|
Vovisoft © 2000. All rights reserved. | ||||
|
Last Updated: 29 th7 2002 |
||||
PcLeHoan
1996 - 2002
Mirror :
http://www.pclehoan.com
Mirror :
http://www.lehoanpc.net
Mirror :
http://www.ktlehoan.com