Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPrivate Sub Text1_Change() If IsNumeric(Text1.Text) = False Then ' vérification On Error Resume Next 'gestion d'erreur Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) ' on retire le dernier caractère (pas numérique) Text1.SelStart = Len(Text1.Text) 'on replace le curseur à la fin End If End Sub
Private Sub txtDegre_KeyPress(KeyAscii As Integer) If InStr(1, "0123456789,.", Chr(KeyAscii)) = 0 And KeyAscii > 27 Then reponse = MsgBox("Ce champ n'accepte que des nombres.", vbExclamation + vbOKOnly, "Erreur") KeyAscii = 0 End If If InStr(1, ",.", Chr(KeyAscii)) > 0 Then KeyAscii = Asc(",") If InStr(1, txtDegre.Text, ",") Then reponse = MsgBox("La marque de la décimale ne peut être utilisée qu'une fois.", vbExclamation + vbOKOnly, "Erreur") KeyAscii = 0 End If End If End Sub
Private Sub txtDegre_Change() If Len(txtDegre.Text) = 0 Then Exit Sub End If Dim debutSelection As Integer debutSelection = txtDegre.SelStart Dim longueurSelection As Integer longueurSelection = txtDegre.SelLength Dim i As Integer Dim texteCorrige As String For i = 1 To Len(txtDegre.Text) If InStr(1, "0123456789,.", Mid(txtDegre.Text, i, 1)) = 0 Then If debutSelection >i Then debutSelection debutSelection - 1 ElseIf InStr(1, ",.", Mid(txtDegre.Text, i, 1)) > 0 Then If InStr(1, texteCorrige, ",") > 0 Then If debutSelection >i Then debutSelection debutSelection - 1 Else texteCorrige = texteCorrige & "," End If Else texteCorrige = texteCorrige & Mid(txtDegre.Text, i, 1) End If Next i If Left(texteCorrige, 1) = "," Then texteCorrige = "0" & texteCorrige If debutSelection 1 Then debutSelection 2 End If txtDegre.Text = texteCorrige txtDegre.SelStart = debutSelection txtDegre.SelLength = longueurSelection End Sub