Visual Basic Text API Routines
SetWindowLong: Special Text Box Styles and Messages
     
Posted:   Saturday January 25, 1997
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Prerequisites
None.

While the standard VB5 text box offers more properties than its VB3 and VB4 counterparts, there remain some styles and messages that can only be set via the API. Styles require changing the edit class default style using SetWindowLong, while the messages use the workhorse SendMessage to add the functionality.

This code shows how to manipulate the styles to convert typed input into upper or lower case, or to force the control to limit its input to numbers only. This last style is explicit .. numbers means numbers .. so no decimal points or commas can be entered. The messages used in this demo set the control to read only (similar to the now-available Locked property in VB5), perform an undo from code (you can testing if undoing is possible using the EM_CANUNDO message first), and an odd one .. EM_SCROLLCARET.

 BAS Module Code
None.

 Form Code
Add a multiline textbox to the form, and set its scrollbars property to vertical. Add a set of Option buttons in a control array (Option1(0)-Option1(3)), a checkbox (Check1) and two command buttons in a control array (Command1(0) and Command1(1)). Finally, add a last command button (Command2) and the following code :

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const GWL_STYLE As Long = (-16)

'edit styles used
Private Const ES_UPPERCASE As Long = &H8&
Private Const ES_LOWERCASE As Long = &H10&
Private Const ES_NUMBER    As Long = &H2000

'edit messages used
Private Const EM_SETREADONLY As Long = &HCF
Private Const EM_UNDO        As Long = &HC7
Private Const EM_SCROLLCARET As Long = &HB7
Private Const EM_SETSEL      As Long = &HB1

Private Declare Function GetWindowLong Lib "user32" _
   Alias "GetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long) As Long
   
Private Declare Function SetWindowLong Lib "user32" _
   Alias "SetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long, _
   ByVal dwNewLong As Long) As Long

Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _ 
   ByVal wMsg As Long, _ 
   ByVal wParam As Long, _
   lParam As Any) As Long
     
Dim defstyle As Long


Private Sub Form_Load()
  
  'store the default textbox style for
  'use later in the option button routines
   defstyle = GetWindowLong(Text1.hwnd, GWL_STYLE)
   
  'force a form show so the SetFocus call
  'in the option routine won't generate
  'an error
   Show
   
  'check the first item
   Option1(0).Value = True  
    
End Sub


Private Sub Command2_Click()

   Unload Me
   
End Sub


Private Sub Text1_GotFocus()

  'uncomment this to have the textbox 
  'select all text on focus
  'Call SendMessage(Text1.hwnd, EM_SETSEL, 0&, ByVal -1&)

End Sub


Private Sub Check1_Click()

  'EM_SETREADONLY
  'Prevents the user from typing or editing
  'text in the edit control. Disables the
  'cut and paste commands on the textbox
  'context menu.
   
  'if the check button is checked (value=1)
  'then state is true, else state is false.
  'Pass state as the wParam member of the
  'message.
   Dim state As Long
   state = Check1.Value = 1
   Call SendMessage(Text1.hwnd, EM_SETREADONLY, _
                    state, ByVal 0&)
                                    
   Text1.SetFocus
   
End Sub


Private Sub Command1_Click(index As Integer)

   Select Case index
      Case 0  'EM_UNDO
              'An application sends an EM_UNDO message to
              'undo the last edit control operation.
              '
              'wParam and lParam are not used and must be 0.
               Call SendMessage(Text1.hwnd, EM_UNDO, 0, ByVal 0)

      Case 1: 'EM_SCROLLCARET
              'An application sends an EM_SCROLLCARET
              'message to scroll the caret into view
              'in an edit control. 
              '
              'To bring the caret into view, wParam and  
              'lParam which are reserved, must be 0.
               Call SendMessage(Text1.hwnd, EM_SCROLLCARET, 0, ByVal 0)
                                    
   End Select
   
   Text1.SetFocus

End Sub


Private Sub Option1_Click(index As Integer)

   
  'reset to the default style for the textbox
   Call SetWindowLong(Text1.hwnd, GWL_STYLE, defstyle)
   
  'apply the new style
   Select Case index
      Case 0: 'nothing to do .. it was done above
      
      Case 1: 'ES_NUMBER
              'Allows only digits to be entered
              'into the edit control.
               Call SetWindowLong(Text1.hwnd, GWL_STYLE, _
                                  defstyle Or ES_NUMBER)

      Case 2: 'ES_UPPERCASE
              'Converts all characters to upper case
              'as they are typed into the edit control.
              'Does not affect existing text.
               Call SetWindowLong(Text1.hwnd, GWL_STYLE, _
                                  defstyle Or ES_UPPERCASE)
      
      Case 3: 'ES_LOWERCASE
              'Converts all characters to lower case
              'as they are typed into the edit control.
              'Does not affect existing text.
               Call SetWindowLong(Text1.hwnd, GWL_STYLE, _
                                  defstyle Or ES_LOWERCASE)
            
   End Select
   
   Text1.SetFocus
   
End Sub
 Comments
Run the project and select an option from either the styles or messages group, then type into the textbox. Setting a style option or sending a message will cause the control to react as indicated.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter