UCase$/LCase$ in Text Box Change Event Inverts Text Property

ID Number: Q84059

1.00

WINDOWS

Summary:

When using the UCase$ or LCase$ functions in Microsoft Visual Basic 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:

- The text property of the text box is being updated by the UCase$ or

LCase$ statement.

- The resulting string created by UCase$ or LCase$ is assigned to the

text property of the text box.

- The above statements appear in the Change event procedure of the

text box.

Every time a key is pressed, the text contents are changed, and the

cursor is placed at the beginning of the line. This causes the

character for your next key press to be inserted at the beginning of

the line rather than the end.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

When 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

---------------------------

1. Start Visual Basic or from the File menu, select New Project (ALT,

F, N) if Visual Basic is already running. Form1 is created by

default.

2. Put a text box (Text1) on Form1 by either double-clicking the text

box control or single clicking on the text box control and drawing

it on Form1.

3. Add the following code to the Text1_Change event procedure:

Sub Text1_Change ()

text1.text = UCase$(text1.text)

End Sub

4. Press F5 to run the program.

Notice that when you try to type information into the text box that it

is entered in reverse order of what you would expect.

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 Sub

When 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 Sub

SelStart 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