Jalaj P. Jha Technical & Miscellaneous Ramblings

23Jan/0752

VB Forms in Hindi Unicode Fonts

Most of the applications I came accross are still using old hindi fonts based on ASCII character sets. And when I tried to build one form in Unicode got to know why! As soon as you paste a unicode string on a control or the property window or the code area the string changes to a series of question marks????????

So should I too start using the same old way? I tried searching the net and this is what I came up with.

Though Visual Basic uses UTF-8 for internally handling the unicode text, the development interface that is provides suffers from a shortcoming that spoils all efforts in developing a unicode font based application. While working on property window, or the code window or saving control's propertybag the Visual Basic takes to convert the strings to charset defined by ANSI loosing the unicode data.

OK, then the alternate solution would be to save the uncode text on a seperate file which would be read and the language text populated during runtime. The catch here is that the controls we intend to use also do the same thing i.e. converting the unicode data to ANSI. However, the corresponding controls available with "Microsoft Forms 2.0 Object Library" are capable of showing the unicode texts.

Create a new project and add a label, commandbutton, textbox, listbox, combobox, checkbox, option button and a toggle button from the Forms 2.0 library controls. The default names of all these would be Label1, CommandButton1, TextBox1, listBox1, ComboBox1, CheckBox1, OptionButton1, ToggleButton1 resp. Change fonts for all these controls to "Arial unicode MS" with font-size 10 or above. "Arial unicode MS" fonts has glyphs for a number of languages including the "Devnagari Script"

Now we would keep our text in an external unicode txt file unitext.txt placed in the same folder as our application. We could have used a resource file too but as our purpose here is just check the feasibility of using unicode fonts on VB forms we will just keep things as discussed. our text file contains the text as below.

लेबल
कमांड बटन
टेक्स्ट बॉक्स
लिस्ट बॉक्स
काँबो बॉक्स
चेक बॉक्स
ऑप्शन बटन
टॉगल बटन

Add a reference to "Microsoft ActiveX Data Objects 2.6 Library". We would use it's Stream object to read the text file.

Paste the code below to load event of the form.

Private Sub Form_Load()

    Dim objStream As New ADODB.Stream
    Dim strFullText As String
    Dim varTextLines As Variant

    objStream.Charset = "Unicode"
    objStream.Open
    objStream.LoadFromFile "unitext.txt"

    strFullText = objStream.ReadText
    varTextLines = Split(strFullText, vbCrLf)

    Label1.Caption = varTextLines(0)
    CommandButton1.Caption = varTextLines(1)
    TextBox1.Text = varTextLines(2)
    ListBox1.AddItem varTextLines(3)
    ComboBox1.Value = varTextLines(4)
    CheckBox1.Caption = varTextLines(5)
    OptionButton1.Caption = varTextLines(6)
    ToggleButton1.Caption = varTextLines(7)

End Sub

The code above reads the unitext.txt file and splitting it with each occurrance of vbCrLf gets the text for each control in form of an array which it later assigns it to the controls. The purpose of this post was to demonstrate using unicode font in VB. See what difference it can make to your aplication.

The VB Form with English Text The same form with Hindi Unicode text
unicodefrm1.jpg unicodefrm2.jpg
Comments (52) Trackbacks (4)
  1. मैं VB6 में हिन्दी फोन्ट मंगल (Script Font) का इस्तेमाल करना चाहता हूं। किन्तु वहां पर केवल ????????? ही आते है।

    कृपया इस समस्या का हल भेजे।


Leave a comment

(required)

  • Twitter
  • Buzz
  • Facebook
  • Orkut
  • Picasa
  • YouTube