UCase$/LCase$ in Text Box Change Event Inverts Text PropertyLast reviewed: June 21, 1995Article ID: Q84059 |
The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0- Microsoft Visual Basic programming system for Windows, version 1.0
SUMMARYWhen using the UCase$ or LCase$ functions in Microsoft Visual Basic for Windows to capitalize text or make text lower case from within the change procedure of a text box, you may encounter unexpected results if the following conditions are true:
MORE INFORMATIONWhen allowing users to enter text into text boxes, it is often desirable to control whether the user enters all uppercase or all lowercase letters. To do this, it would seem that putting a UCase$ or LCase$ statement in a text box Change event would allow you to enter only uppercase or lowercase letters into the text box. However, each time you press a key, the Change event fires and the cursor is brought back to the beginning of the text box as a result of assigning the Text property a new string.
Steps to Reproduce Behavior
An alternative method of changing all contents of the text box to capital letters is to change the KeyAscii code of the typed information in the text box KeyPress event as follows:
Sub Text1_KeyPress (KeyAscii As Integer) ' Check to see if key pressed is a lower case letter. If KeyAscii >= 97 And KeyAscii <= 122 Then 'If it is lowercase, change it to uppercase. KeyAscii = KeyAscii - 32 End If End SubWhen you run the above code, the letters typed into the text box are immediately changed to capital letters and are entered correctly as you type them in. Another alternative method of changing the contents of the text box to uppercase letters is to add the following code to the Change event for the text box:
Sub Text1_Change () ' Get the current position of the cursor. CurrStart = Text1.SelStart ' Change the text to capitals. Text1.Text = UCase$(Text1.Text) ' Reset the cursor position. Text1.SelStart = CurrStart End SubSelStart sets or returns the starting point of text selected, and indicates the position of the insertion point if no text is selected.
|
Additional reference words: 1.00 2.00 3.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |