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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
Public Class FTextbox
Inherits System.Windows.Forms.TextBox
Private _SeparateurDecimal As Char
Private WithEvents _Calendrier As System.Windows.Forms.DateTimePicker
Public Enum liste_texte
texte = 0
nombre_entier = 1
nombre_decimal_2_decimales = 2
nombre_decimal_4_decimales = 3
date_ = 4
End Enum
Public Sub New()
Me.TexteType = liste_texte.texte
_SeparateurDecimal = CChar(My.Application.Culture.NumberFormat.NumberDecimalSeparator)
End Sub
Private _TexteType As liste_texte
Public Property TexteType() As liste_texte
Get
Return _TexteType
End Get
Set(ByVal value As liste_texte)
If _TexteType = value Then Exit Property ' pas de changement
_TexteType = value
Me.Text = "" ' quand cette propriété est modifiée, on efface
If value = liste_texte.date_ Then
Me.Text = "__/__/____"
If _Calendrier Is Nothing Then _Calendrier = New System.Windows.Forms.DateTimePicker
Affiche_Calendrier()
Else
If _Calendrier IsNot Nothing Then
_Calendrier.Dispose()
_Calendrier = Nothing
End If
End If
End Set
End Property
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
'la touche suppr ne déclenche pas de keypress je crois
Select Case _TexteType
Case liste_texte.texte
'on accepte tout donc on ne fait rien
Case liste_texte.nombre_entier
GestionKeyPressNombre(e, 0) ' e est passé en byref pour que les modifications distantes soit gardées
Case liste_texte.nombre_decimal_2_decimales
GestionKeyPressNombre(e, 2)
Case liste_texte.nombre_decimal_4_decimales
GestionKeyPressNombre(e, 4)
Case liste_texte.date_
e.KeyChar = CChar("")
End Select
MyBase.OnKeyPress(e)
End Sub
Private Sub GestionKeyPressNombre(ByRef e As System.Windows.Forms.KeyPressEventArgs, ByVal nombre_decimales_acceptees As Byte)
Try
If Asc(e.KeyChar) = 8 Then ' touche del
If Me.SelectionStart = 0 AndAlso Me.Text.Chars(Me.SelectionLength) = _SeparateurDecimal Then
'si on sélectionne tout ce qui est avant une virgule et qu'on efface ca devrait faire ",xx" alors on fait comme si qu'on avait tapé 0 pour faire "0,xx"
e.KeyChar = CChar("0")
Else
Exit Sub
End If
End If
If e.KeyChar = "." Or e.KeyChar = "," Then e.KeyChar = _SeparateurDecimal
If Not ("0123456789" & _SeparateurDecimal).Contains(e.KeyChar) Then 'le caractère n'est ni un chiffre ni la virgule
e.KeyChar = CChar("")
Exit Sub
End If
If e.KeyChar = _SeparateurDecimal And nombre_decimales_acceptees = 0 Then 'la virgule est interdite pour un nombre entier
e.KeyChar = CChar("")
Exit Sub
End If
'le caractère n'est pas encore tapé dans le textbox dans cet évènement
'il ne sera pas forcément au bout du textbox, ca dépend d'où est le curseur et faut faire attention aussi à ce qui est sélectionné
Dim texte_que_ca_sera As String
If Me.SelectionLength = 0 Then
texte_que_ca_sera = Me.Text.Substring(0, Me.SelectionStart) & e.KeyChar & Me.Text.Substring(Me.SelectionStart, Me.Text.Length - Me.SelectionStart)
Else
texte_que_ca_sera = (Me.Text.Substring(0, Me.SelectionStart) & e.KeyChar) & Me.Text.Substring(Me.SelectionStart + Me.SelectionLength, Me.Text.Length - Me.SelectionStart - Me.SelectionLength)
End If
If Not IsNumeric(texte_que_ca_sera) Then
e.KeyChar = CChar("")
Exit Sub
End If
If Not texte_que_ca_sera.Contains(_SeparateurDecimal) Then Exit Sub ' pas de problème possible
'vérification du nombre de décimales
Dim nombre_de_chiffres_apres_la_virgule As Integer
nombre_de_chiffres_apres_la_virgule = (texte_que_ca_sera.Substring(texte_que_ca_sera.IndexOf(_SeparateurDecimal), _
texte_que_ca_sera.Length - texte_que_ca_sera.IndexOf(_SeparateurDecimal) - 1)) _
.Length
If nombre_de_chiffres_apres_la_virgule > nombre_decimales_acceptees Then
e.KeyChar = CChar("")
Exit Sub
End If
Catch ex As Exception
e.KeyChar = CChar("")
Exit Sub
End Try
End Sub
Private Sub Affiche_Calendrier()
Me.Controls.Add(_Calendrier)
_Calendrier.Top = -2
_Calendrier.Left = Me.Width - 22
_Calendrier.Width = 18
_Calendrier.Height = Me.Height
_Calendrier.DropDownAlign = LeftRightAlignment.Right
End Sub
Private Sub AfficheDate(ByVal sender As Object, ByVal e As System.EventArgs) Handles _Calendrier.ValueChanged
Me.Text = _Calendrier.Value.ToShortDateString
End Sub
End Class |
Partager