Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text1_keypress event

Status
Not open for further replies.

pdbowling

Programmer
Mar 28, 2003
267
0
0
US
Hey everyone,
Does anyone know of a good snippet of code to make each keystroke a capital letter in a text box?

Side notes:
Caps lock on the keyboard is not an option..... we have a hand scanner that scans several thousand different upper case bar codes that would get flipped to lower case if caps lock was on.. and.... crystal reports is case sensitive as is the database.

Thanka all
PB
 
Text1.text = ucase(text1.text)

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
or,

Code:
If KeyAscii >= 97 And KeyAscii <= 122 Then KeyAscii = KeyAscii - 32


Thanks and Good Luck!

zemp
 
Hey, thanks guys.
I just put text1.text = ucase(text1.text) in the LostFocus event instead of keyPress. I got some really weird behavior with keyPress.
Thanks
PB
 
If you want to do it that way you would be better off using the validate event.

Thanks and Good Luck!

zemp
 
Here's how it's done with a KeyPress event...


Private Sub Text1_KeyPress(KeyAscii As Integer)

'KeyAscii stores the ASCII character code

'ASCII 97 to 122 = lowercase a thru z
'if within this range change to uppercase
'otherwise accept normal input

If KeyAscii > 96 And KeyAscii < 123 Then
Text1.Text = Text1.Text & UCase(Chr$(KeyAscii))
Text1.SelStart = Len(Text1.Text)


'set keyascii to 0 so the original passed ascii code
'isn't output to the textbox, only the above changed
'uppercase result which we added manually

KeyAscii = 0
End If

End Sub

 
Here's how it's done simpiler and better with a KeyPress event...

Private Sub Text11_KeyPress(KeyAscii As Integer)
If KeyAscii > 96 And KeyAscii < 123 Then KeyAscii = KeyAscii - 32
End Sub

Notice you do not have a problem marking text and pressing a letter to replace the marked text.

Anyways, someone may find fault with that also.

Do not use the LostFocus, the KeyDown ot the KeyPress events.

LostFocus will not always work.

And, you need extra, different, code in the KeyDown and KeyPress events to capture Pasting.

The only two events which seem to me best to use is either the Validate event, or, if the keys should change when focus is still on the control, the Change event.
Use the Change event, but add a static boolean to cancel the Change event from being executed a second time, in order to avoid conflict with other code in the event, or other code being called repeatedly(unless the UCase is the only thing being done in the event). So, if needed, remove the comment markers:

Private Sub Text11_Change()
'Static bGetOut As Boolean
'If bGetOut Then Exit Sub
'bGetOut = True
Text11.Text = UCase$(Text11.Text)
'bGetOut = False
End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top