Inserimento solo di numeri in una TextBox o ComboBox
Per controllare se nell'inserimento in una Textbox
o in una ComboBox etc., vengano inseriti solo
numeri possiamo utilizzare diversi metodi. Ve ne presento alcuni.
Le Routine dei primi 4 metodi devono essere richiamate da un evento KeyPress.
1° Metodo:
'*********************************************
'Dichiarare la costante "cifre" in un modulo
'Public Const cifre As String = "01234567890.,"
'*********************************************
Public Sub SoloNumeri(KeyAscii As Integer)
If InStr(cifre & Chr(8) & Chr(13), Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Testato su: Tutti i Sistemi operativi
2° Metodo:
'oppure senza dichiarazione della costante "cifre"
Public Sub SoloNumeriB(KeyAscii As Integer)
Dim cifre As String
cifre = "0123456789.," & Chr(8) & Chr(13)
If InStr(cifre, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Testato su: Tutti i Sistemi operativi
3° Metodo:
Public Sub SoloNumeri2(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 13 Then 'tasto backspace
If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End If
End Sub
Testato su: Tutti i Sistemi operativi
4° Metodo:
Public Sub SoloNumeri3(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 'numeri
Case 8, 13 'backspace e invio
Case Else
KeyAscii = 0
End Select
End Sub
Testato su: Tutti i Sistemi operativi
5° Metodo:
Private Function SoloNumeri4(strText As String) As Boolean
SoloNumeri4 = CBool(strText = "" _
Or strText = "-" Or strText = "-," _
Or strText = "," Or IsNumeric(strText))
End Function
Testato su: Tutti i Sistemi operativi