CodeItBetter Programming Another VB Programming Blog

How to make a TextBox automatically capitalize input using the API

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'Controls - How to make a TextBox automatically capitalize input using the API
'Use the SetWindowLong API function to make the TextBox automatically convert 
'text into upper case.

Private Sub SetAllCaps(ByVal txt As TextBox)
    Const GWL_STYLE = (-16)
    Const ES_UPPERCASE = &H8&
    Dim style As Long
    style = GetWindowLong(txt.hWnd, GWL_STYLE)
    style = style Or ES_UPPERCASE
    SetWindowLong txt.hWnd, GWL_STYLE, style
End Sub
 
'This method has a couple of advantages.

'1. It is handled at a lower level than the VB event handler so it is more
'efficient. That's not a huge deal since usually it's a user entering text so
'it's going to be fast enough either way.

'2. It's "set and forget." Once you set the flag, you don't need to have the VB
'code cluttering things up. You don't need to write separate event handlers for
'each TextBox.
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.