Visual Basic - Inputbox solo numeros

Life is soft - evento anual de software empresarial
 
Vista:

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
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Inputbox solo numeros

Publicado por SetFocus (183 intervenciones) el 30/05/2005 00:02:24
Inputbox no te permite limitar el ingreso de solo numeros, pero podrias hacer algo asi:

1
2
3
4
Dim Numero As String
Do
Numero = InputBox("numero:")
Loop Until IsNumeric(Numero)

Es una buena solucion que encontre por ahi. Suerte y Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil
Val: 14
Ha aumentado 1 puesto en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

RE:Inputbox solo numeros

Publicado por SuNcO (599 intervenciones) el 09/07/2005 20:54:10
Ya respondi eso en otra ocasion pero no la encuentro.. asi que mas facil aqui

Ocupas 2 botones.. 1 para que acepte solo numeros y el 2 es para que salga tipo password (osea con asteriscos)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
 
' ------------------------------------------------------------------

en un modulo :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Inputbox solo numeros

Publicado por Churrua (1 intervención) el 16/05/2017 13:32:50
Genial, funciona perfecto!!! Simple y efectivo.
Muchas gracias.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Inputbox solo numeros

Publicado por D Sandoval (1 intervención) el 01/10/2019 18:37:15
Buen día, encontré lo siguiente:

Dim Numero As Double 'o Integer
Numero = Application.InputBox("numero:", Type:=2)


'Pero necesito que sean positivos (Ley de no negatividad).

Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar