Measurement Studio for VB6

cancel
Showing results for 
Search instead for 
Did you mean: 

I do not want the CWNumEdit control to accept characters.

I do not want the CWNumEdit control to accept characters. I have set the built-in format style of the CWNumEdit control to Number, but it is still possible to type the characters a,b,c,d,e and f until I press the key "Enter".
0 Kudos
Message 1 of 3
(2,927 Views)
There's not an easy way to do this with the CWNumEdit. You could do it, however, by subclassing the edit control of the CWNumEdit. Here are some steps to do it.

First, download olelib.tlb and unzip it. Then create a new VB project, go to Project->References, click Browse, then browse to the olelib.tlb file that was unzipped from the zip file downloaded above.

Now add a module (.base file) to your project and paste the following code into it:

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long _
) As Long

Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" ( _
ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) As Long

Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String _
) As Long

Public Const GWL_WNDPROC = (-4)
Public Const WM_CHAR = &H102

Global lpWndProc As Long

Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WM_CHAR Then
If ((wParam >= 97) And (wParam <= 102)) Then
WndProc = -1
ElseIf ((wParam >= 65) And (wParam <= 70)) Then
WndProc = -1
Else
WndProc = CallWindowProc(lpWndProc, hWnd, uMsg, wParam, lParam)
End If
Else
WndProc = CallWindowProc(lpWndProc, hWnd, uMsg, wParam, lParam)
End If
End Function

Go to your form, drop a CWNumEdit on it, go to the code view, and paste this code into it:

Private Sub Form_Load()
Dim window As IOleInPlaceObject
Set window = CWNumEdit1.Object
Debug.Assert Not window Is Nothing

Dim numEditWnd As Long
numEditWnd = window.GetWindow
Debug.Assert Not numEditWnd = 0

Dim editWnd As Long
editWnd = FindWindowEx(numEditWnd, 0, "edit", "")
Debug.Assert Not editWnd = 0

lpWndProc = SetWindowLong(editWnd, GWL_WNDPROC, AddressOf WndProc)
End Sub

Run the project and you should see that the CWNumEdit no longer accepts the alphabetic characters. Hope this helps.

- Elton
0 Kudos
Message 2 of 3
(2,927 Views)
I just realized that some of the code got messed up when the message was HTML-ized. The WndProc has = signs where it should be <= signs. Here's the correct version of the WndProc function:

Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WM_CHAR Then
If ((wParam >= 97) And (wParam <= 102)) Then
WndProc = -1
ElseIf ((wParam >= 65) And (wParam <= 70)) Then
WndProc = -1
Else
WndProc = CallWindowProc(lpWndProc, hWnd, uMsg, wParam, lParam)
End If
Else
WndProc = CallWindowProc(lpWndProc, hWnd, uMsg, wParam, lParam)
End If
End Function

- Elton
0 Kudos
Message 3 of 3
(2,927 Views)