Inputbox solo numeros
Publicado por Jhon Alexander (58 intervenciones) el 29/05/2005 19:52:17
Como puedo forzar un inputbox para que solo acepte numero
Gracias
Gracias
Valora esta pregunta


0
Dim Numero As String
Do
Numero = InputBox("numero:")
Loop Until IsNumeric(Numero)
Private Sub Command1_Click()
Command1.Tag = "Uhm"
hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf Manipulate, hInst, Thread)
Resultado = InputBox("Intenta escribir letras", "Solo numeros")
Command1.Tag = ""
End Sub
Private Sub Command2_Click()
Command2.Tag = "Uhm"
hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf Manipulate, hInst, Thread)
Resultado = InputBox("Se ven tipo password", "Tipo Password")
Command2.Tag = ""
End Sub
' ------------------------------------------------------------------
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
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const GWL_STYLE = (-16)
Const ES_NUMBER = &H2000&
Const KEY_MASK = 42&
Const EM_SETPASSWORDCHAR = &HCC
' used for placing the hook
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Const GWL_HINSTANCE = (-6)
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOZORDER = &H4
Public Const SWP_NOACTIVATE = &H10
Public Const HCBT_ACTIVATE = 5
Public Const WH_CBT = 5
Public hHook As Long
' used for locating and changing the buttons
Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long
Public Declare Function SetWindowText Lib "user32.dll" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Public Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public ButtonText(0 To 3) As String
Public Sub SetNumber(hwnd As Long, Flag As Boolean)
Dim curstyle As Long, newstyle As Long
If Flag = True Then
curstyle = GetWindowLong(hwnd, GWL_STYLE)
curstyle = curstyle Or ES_NUMBER
newstyle = SetWindowLong(hwnd, GWL_STYLE, curstyle)
Else
SendMessage hwnd, EM_SETPASSWORDCHAR, KEY_MASK, 0&
End If
End Sub
' function called by hook
Public Function Manipulate(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim Btn(0 To 3) As Long
Dim ButtonCount As Integer
Dim T As Integer
If lMsg = HCBT_ACTIVATE Then
Btn(0) = FindWindowEx(wParam, 0, vbNullString, vbNullString)
Dim cName As String, Length As Long
For T = 1 To 3
Btn(T) = FindWindowEx(wParam, Btn(T - 1), vbNullString, vbNullString)
' no more windows found
If Btn(T) = 0 Then Exit For
Next T
For T = 0 To 3
If Btn(T) <> 0 And Btn(T) <> wParam Then
cName = Space(255)
Length = GetClassName(Btn(T), cName, 255)
cName = Left(cName, Length)
'Debug.Print cName
If UCase(cName) = "EDIT" Then
If Form1.Command1.Tag <> "" Then
SetNumber Btn(T), True
Else
SetNumber Btn(T), False
End If
End If
'If UCase(cName) = "BUTTON" Then
' a button
' SetWindowText Btn(T), ButtonText(ButtonCount)
' ButtonCount = ButtonCount + 1
'End If
End If
Next T
'Release the CBT hook
UnhookWindowsHookEx hHook
End If
Manip = False
End Function