Search code examples
vb.netcomlocalizationvb6

.NET COM usercontrol does not set font or language properly


I have created a COM VB.NET (v3.5 under VS2013) usercontrol which i have included in an old VB6 project. The control communicates with its VB6 host just fine. It works as expected, opens the DB, reads / writes stuff. Everything is OK except one thing, which i'm not sure what it is. It seems i cannot properly set either its font(?) or its language(?) to Greek in order to be able to type Greek characters in a contained textbox.

This usercontrol contains several other .NET controls. Listboxes, buttons and textboxes. I read stuff from DB, set the text property of all those controls to the contents of the database and Greek are shown properly. If i try to switch the O/S language to Greek and type something in a textbox, strange characters are shown instead of the proper Greek ones (i assume from another codepage). I have programmatically changed its font to "[Font: Name=Arial, Size=9, Units=3, GdiCharSet=161, GdiVerticalFont=False]". GdiCharSet=161 is Greek. It should work. And it does work from within the .NET environment or from a test .NET exe. I can type Greek in the textbox. When i try to use it from within the VB6 program, it seems switching to Greek and typing Greek chars is not possible. Loading Greek from DB and showing them is no problem though.

I have also tried setting GdiCharSet to 0, 1, 2 (yup, it did show Symbols as expected) and to change the culture in the usercontrol constructor but no luck:

System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("el-GR")
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("el-GR")

Any ideas someone, what to check / try?


Solution

  • Wow... After spending more hours than i could spare on this matter, i have finally found a solution.

    Private Sub txtMessage_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtMessage.KeyPress
        If InputLanguage.CurrentInputLanguage.Culture.Equals(New Globalization.CultureInfo("el-GR")) Then
            FixKeyChar(e)
        End If
    End Sub
    
    Public Sub FixKeyChar(ByRef e As KeyPressEventArgs)
        Dim myUnicodeBytes As Byte() = Encoding.Unicode.GetBytes(e.KeyChar)
        Dim myUTF32Bytes As Byte() = Encoding.Convert(Encoding.Default, Encoding.UTF32, myUnicodeBytes)
        e.KeyChar = ChrW(myUTF32Bytes(1) * 256 + myUTF32Bytes(0))
    End Sub
    

    I hope it proves to be helpful for some other poor soul messing with these things...