LookText



Private Sub Text1_KeyPress(KeyAscii As Integer)
'questo esempio limita ad un intervallo da ZERO a duecento.

'Se vuoi limitare ad un campo da uno o qualcosa del genere

'stai attento che non permette la stringa vuota nel campo

'di testo perché Val("") e' minore e da' errore

'se vuoi ovviare a questo annulli il controllo se TmpStr

'e' di lunghezza zero

If Not IsNumeric(Chr(KeyAscii)) Then
If KeyAscii <> Asc(vbTab) And KeyAscii <> Asc(vbBack) _
And KeyAscii <> Asc(vbCr) And KeyAscii <> Asc(vbLf) Then
''carattere non accettato

KeyAscii = 0
Exit Sub
End If
End If
''controllo campo di validita': ad esempio 1-200

Dim TmpStr As String
If KeyAscii <> Asc(vbBack) Then
TmpStr = Left(Text1.Text, Text1.SelStart)
TmpStr = TmpStr & Chr(KeyAscii)
TmpStr = TmpStr & Right(Text1.Text, Len(Text1.Text) _
- Text1.SelStart - Text1.SelLength)
Else ''comportamento particolare del backspace
TmpStr = Left(Text1.Text, Text1.SelStart)
If Text1.SelLength > 0 Then
''il backspace cancella la selezione

TmpStr = TmpStr & Right(Text1.Text, Len(Text1.Text) -
Text1.SelStart - Text1.SelLength)
Else
'il backspace cancella il carattere a sinistra del cursore

TmpStr = Left(TmpStr, Len(TmpStr) - 1)
TmpStr = TmpStr & Right(Text1.Text, Len(Text1.Text) -
Text1.SelStart - Text1.SelLength)
End If
End If
If Not (Val(TmpStr) >= 0 And Val(TmpStr) <= 200) Then
KeyAscii = 0
End If
End Sub


(looktext.html)- by Paolo Puglisi - Modifica del 25/3/2019