Advertisement
7_2009-2012 Windows API Call/ Explanation #224643

Force Numeric Entry in a Text Box Using API

This routine will cause a textbox control to accept only numeric input. Any other input (including the '-' and '.' keys) will be ignored. Note that it is still possible to paste non-numeric data into the textbox. There have been plenty of examples of this in the last couple of days - in the comments section of one of these someone suggested using the API. So, for anyone that is interested, this is one way of doing it... I've put this in the intermediate category only because it uses API functions - otherwise it's straightforward code. I've only tested this in VB6.0 on Win2k, but it should work on any Windows platform from Win95 up.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Private Sub ForceNumeric(Box As TextBox)
  On Error GoTo Catch
  Dim nStyle As Long
  
  nStyle = GetWindowLong(Box.hWnd, GWL_STYLE)
  Call SetWindowLong(Box.hWnd, GWL_STYLE, nStyle Or ES_NUMBER)
  GoTo Finally
  
Catch:
  Call MsgBox(Err.Description, vbCritical Or vbOKOnly, "Error")
Finally:
End Sub
Original Comments (3)
Recovered from Wayback Machine