OnlyNumbers




Function FiltraTasti(cContr As Control, iCodiceTasto _
As Integer, iMaxLen as Integer)
If iCodiceTasto = vbKeyBack Then
FiltraTasti = iCodiceTasto
Exit Function
End If
If Len(cContr.Text) >= iMaxlen Then
If cContr.SelLength = 0 Then
FiltraTasti = 0
Exit Function
End If
End If
If Not IsNumeric(Chr(iCodiceTasto)) _
And iCodiceTasto <> 46 Then '46 = punto
iCodiceTasto = 0
Else
If InStr(cContr.Text, ".") > 0 And iCodiceTasto = 46 Then
iCodiceTasto = 0
Else
FiltraTasti = iCodiceTasto
End If
End If
End Function

Questa funzione si richiama nell'evento KeyPress del controllo in
questo modo:
Private Sub txtTesto_KeyPress(KeyAscii As Integer)
KeyAscii = FiltraTasti(txtTesto, KeyAscii, 10)
End Sub

Inserire un controllo nell'evento KeyPress.
Per gestirlo creare una funzione a cui passare il
codice del tasto premuto (KeyAscii) ed il controllo
stesso, in modo da poterla usare anche con dei
combobox dove non e' possibile specificare la
proprieta' MaxLenght.










( onlynumbers.html )- by Paolo Puglisi - Modifica del 17/12/2023