
Limiting Access to Text Boxes, List Boxes and Combo Boxes
Try changing the routine to (tested in VB5/VB6 on Win98):
Public Sub NumOnly(ctl As Control)
Dim style As Long
Dim hWndEdit As Long
If TypeOf ctl Is TextBox Then
'textbox, so just need the handle
hWndEdit = ctl.hwnd
ElseIf TypeOf ctl Is ComboBox Then
'combobox, so need to obtain the handle
'of its child edit part
hWndEdit = FindWindowEx(ctl.hwnd, 0&, vbNullString, vbNullString)
End If
'if a valid handle
If hWndEdit Then
'get the edit control style
style = GetWindowLong(hWndEdit, GWL_STYLE)
'if its not already es_number, set it
If style And Not ES_NUMBER Then
style = style Or ES_NUMBER
style = SetWindowLong(hWndEdit, GWL_STYLE, style)
End If
End If
End Sub
--
Randy Birch, MVP Visual Basic
http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/
To assist in maintaining this thread for the benefit of
others, please post any response to this newsgroup.
Quote:
>Urgent Help Required, All viable suggestions greatly appreciated
>I am tyring to limit user input on a text box and combo box to be numberic
>only.
>Any suggestions,
>I was using the following but this doesnt seem to work on combos or my text
>boxes any more
>Private Const GWL_STYLE = (-16)
>Private Const ES_LOWERCASE = &H10&
>Private Const ES_UPPERCASE = &H8&
>Private Const ES_NUMBER = &H2000&
>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
>Public Sub NumOnly(txtBox As TextBox)
>Dim lStyle As Long
>lStyle = GetWindowLong(txtBox.hwnd, GWL_STYLE)
>lStyle = lStyle Or ES_NUMBER
>lStyle = SetWindowLong(txtBox.hwnd, GWL_STYLE, lStyle)
>End Sub