VB and VBA Users Source Code: Changing the type of input a textbox can receive
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Changing the type of input a textbox can receive
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, May 30, 2001
Hits:
770
Category:
Windows API
Article:
The following code demonstrates how to alter the type of user input a textbox can receive eg, limit a textbox to lowercase text or to only numbers. PLACE CODE IN A STANDARD MODULE: Option Explicit 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 Const ES_UPPERCASE = &H8&, ES_LOWERCASE = &H10&, EM_SETREADONLY = &HCF, EM_UNDO = &HC7, EM_SCROLLCARET = &HB7, ES_NUMBER As Long = &H2000 Public Enum eTextboxStyle tbLowercase = ES_LOWERCASE tbUppercase = ES_UPPERCASE tbNumber = ES_NUMBER tbReadOnly = EM_SETREADONLY tbScrollCaret = EM_SCROLLCARET tbUndo = EM_UNDO End Enum 'Purpose : Sets the style bit of a textbox 'Inputs : lhwndTextBox The handle to the textbox ' eStyle An enumerated type containing the style bit to set. 'Outputs : Returns the original style bit for the textbox 'Author : Andrew Baker 'Date : 25/04/2001 'Notes : Function TextBoxSetStyle(lhwndTextBox As Long, eStyle As eTextboxStyle) As Long Const GWL_STYLE As Long = (-16) 'Return the current textbox style TextBoxSetStyle = GetWindowLong(lhwndTextBox, GWL_STYLE) 'set the textbox style bit Call SetWindowLong(lhwndTextBox, GWL_STYLE, TextBoxSetStyle Or eStyle) End Function PLACE CODE IN A FORM: 'Demonstration routine (place in form which contains a textbox) Private Sub Form_Load() 'Limit the textbox to receiving numeric input. TextBoxSetStyle Text1.hwnd, tbNumber End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder