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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| '---------------------------------------------------------------------------------------
' Module : myNumericBox
' Créé le : 26/02/2008 21:32
' Auteur : Maxence Hubiche (mhubiche@club-internet.fr)
' Objet : Permettre la saisie d'un numérique dans un MsForms.TextBox par mappage
' et interception des évènements
'---------------------------------------------------------------------------------------
Option Explicit
'Fonctions pour déterminer le séparateur de décimales
Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
Private Declare Function GetUserDefaultLCID Lib "kernel32" () As Long
Private Const LOCALE_SDECIMAL = &HE
'Enumération des cas particuliers à traiter
Enum eMyType
eMyType_Entier
eMyType_Entier_NonSigne
eMyType_Reel
eMyType_Reel_NonSigne
End Enum
'Mappage du contrôle TextBox
Private WithEvents m_TB As MSForms.TextBox
'Propriétés 'Privées' de la classe
Private m_Type As eMyType
Private m_ValidColor As Long
Private m_InvalidColor As Long
'Constantes pour les numériques
Const C_ENTIERS As String = "0123456789"
Const C_SIGNE As String = "-"
Private m_SEPARATOR As String
Private m_BAD_SEPARATOR As String
'Prévoir 2 évènements en retour
Public Event AvantAppuieTouche(Touche As MSForms.ReturnInteger)
Public Event ApresAppuieTouche(Touche As MSForms.ReturnInteger)
'Méthode visible de la classe
'Sert à Mapper la textBox à la classe
Public Function Definir(TB As MSForms.TextBox, myTYpe As eMyType, Optional ValidColor As Long = vbGreen, Optional InvalidColor As Long = vbRed) As TextBox
Set m_TB = TB
m_ValidColor = ValidColor
m_InvalidColor = InvalidColor
m_Type = myTYpe
End Function
Private Sub Class_Initialize()
m_SEPARATOR = Separator
If m_SEPARATOR = "." Then
m_BAD_SEPARATOR = ","
Else
m_BAD_SEPARATOR = "."
End If
End Sub
'Interception de l'évènement du contrôle mappé (KeyDown)
' pour les deux touches BackSpace et Suppr
Private Sub m_TB_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Const BACKSPACE As Long = 8
Const SUPPRIME As Long = 46
Select Case KeyCode
Case BACKSPACE, SUPPRIME
m_TB.BackColor = m_ValidColor
Case Else
'rien à faire
End Select
End Sub
'Interception de l'évènement du contrôle mappé (KeyPress)
' pour toutes les touches de saisie
Private Sub m_TB_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim blnValide As Boolean
RaiseEvent AvantAppuieTouche(KeyAscii)
Select Case m_Type
Case eMyType_Entier
blnValide = Controle(Chr(KeyAscii), C_ENTIERS, True)
Case eMyType_Reel
blnValide = Controle(Chr(KeyAscii), C_ENTIERS & m_SEPARATOR, True)
Case eMyType_Entier_NonSigne
blnValide = Controle(Chr(KeyAscii), C_ENTIERS, False)
Case eMyType_Reel_NonSigne
blnValide = Controle(Chr(KeyAscii), C_ENTIERS & m_SEPARATOR, False)
End Select
If blnValide Then
m_TB.BackColor = m_ValidColor
Else
m_TB.BackColor = m_InvalidColor
KeyAscii = 0
End If
RaiseEvent ApresAppuieTouche(KeyAscii)
End Sub
'Méthode privée de la classe qui s'occupe de la validation de la saisie
Private Function Controle(Code As String, ControlCode As String, Signed As Boolean) As Boolean
Dim strCompare As String
Dim strTemp As String
Dim dblValue As Double
Dim blnValue As Boolean
If m_TB.SelStart = 0 And Signed Then
strCompare = C_SIGNE & ControlCode
Else
strCompare = ControlCode
End If
On Error GoTo GestErr
strTemp = Replace(m_TB.Text & Code & "0", m_BAD_SEPARATOR, m_SEPARATOR)
dblValue = CDbl(strTemp)
blnValue = IsNumeric(strTemp)
blnValue = blnValue And InStr(1, strCompare, Code, vbTextCompare) <> 0
Controle = blnValue
FinProg:
Exit Function
GestErr:
Controle = False
Resume FinProg
End Function
'Méthode privée pour déterminer le séparateur de décimales
Private Function Separator() As String
Dim lngResultat As Long
Dim buffer As String
Dim pos As Integer
Dim locale As Long
'récupère l'identifiant de l'information locale de type utilisateur
locale = GetUserDefaultLCID()
'renvoie le nombre de caractères nécessaire pour recevoir la valeur du paramètre demandé
lngResultat = GetLocaleInfo(locale, LOCALE_SDECIMAL, buffer, 0)
buffer = String(lngResultat, 0)
GetLocaleInfo locale, LOCALE_SDECIMAL, buffer, lngResultat
pos = InStr(buffer, Chr(0))
If pos > 0 Then Separator = Left(buffer, pos - 1)
End Function |
Partager