Microsoft KB Archive/189480

= PRB: RichTextBox Control Saves Locale Setting When Created =

Article ID: 189480

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q189480



SYMPTOMS
Text entered into a RichTextBox control at run-time uses a keyboard layout that is different than the current setting.



CAUSE
When the RichTextBox control was initially added to the form, the then- current keyboard setting was saved with the text in the text property of the control.



RESOLUTION
There are several possible workarounds depending on the version of the control you are working with.

With version 6.0 of the control there are two workarounds:


 * 1) When adding the control to a form, delete the text in the text property. If the control has no text, the layout setting will not be saved.
 * 2) Select all the text in the RichTextBox control and set the keyboard locale to the language desired.

With Version 5.0 and earlier of the control, use the following code to check the keyboard layout of the form and use it as the check value. Then check the keyboard layout of the RichTextBox control and change it to the form's value if different.

Step-by-Step Example
  Add the following code to General Declarations section of the form module: Option Explicit

Private Declare Function GetKeyboardLayoutName Lib "user32" Alias _ "GetKeyboardLayoutNameA" (ByVal pwszKLID As String) As Long

Private Declare Function LoadKeyboardLayout Lib "user32" Alias _ "LoadKeyboardLayoutA" (ByVal pwszKLID As String, _     ByVal flags As Long) As Long

Dim K1 As String * 10 Dim KybdOriginal As Long   Add the following code to the form Load event procedure: K1 = Space$(10) KybdOriginal = GetKeyboardLayoutName(K1)   Add the following code to the GotFocus event procedure of the RichTextBox control: Dim K2 As String * 10 Dim KybdCode As Long

K2 = Space$(10) KybdCode = GetKeyboardLayoutName(K2)

If (K1 <> K2) Then KybdCode = LoadKeyboardLayout(K1, 1) 

This should reset the keyboard layout setting for the RichTextBox control to match the layout used by the form.



STATUS
This behavior is by design. The RichTextBox control saves the language setting when text is saved.



MORE INFORMATION
When a RichTextBox control is added to a form, default text is added to the text property. When the form is saved the RTF data format includes the keyboard settings as part of the font information.

Version 6.0 of the control does not save the keyboard setting if the text property of the control is empty. Previous versions of the control saved the setting regardless of the contents of the text property.

The keyboard (Locale) setting can be changed in Control Panel/Regional Settings.

