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
| Public Class MyTextbox
Inherits MetroTextBox
Public Sub New()
UseCustomBackColor = True
UseCustomForeColor = True
BackColor = Color.FromKnownColor(KnownColor.ButtonHighlight)
ForeColor = Color.Black
Height = 23
Width = 100
ShowFocus = True
End Sub
Private _mesAccKeyChar() As Integer
Private _Text As String
Private _parent As Control
<Category("2M Management Studio")> <Description("Met en surbrillance le control actif")> <DefaultValue(True)>
Public Property ShowFocus As Boolean
<Category("2M Management Studio")> <Description("Choix de la Couleur lorsque le contrôle est actif")> <DefaultValue(KnownColor.GrayText)>
Public Property FocusColor As Color
<Category("2M Management Studio")> <Description("Choix du type de Text (Numérique, Date, texte ou monnaitaire)")> <DefaultValue(TextboxType.IsText)>
Public Property Type As TextboxType
<Category("2M Management Studio")> <Description("Choix du nombre de chiffres après la virgule")> <DefaultValue(NbDecimal.Deux)>
Public Property NbDecimal As NbDecimal
Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
MyBase.OnKeyPress(e)
If Type <> TextboxType.IsText Then
Select Case Type
Case TextboxType.IsDate
_mesAccKeyChar = {8, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57}
Case Else
_mesAccKeyChar = {8, 44, 45, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57}
End Select
If Asc(e.KeyChar) = 46 Then e.KeyChar = ","
If Not _mesAccKeyChar.Contains(Asc(e.KeyChar)) Then
e.Handled = True
Else
e.Handled = False
End If
End If
End Sub
Protected Overrides Sub OnTextChanged(e As EventArgs)
MyBase.OnTextChanged(e)
If Type = TextboxType.IsDate Then
Select Case Text.Length
Case 3
_Text = Strings.Left(Text, 2)
_Text += "/" + Strings.Right(Text, 1)
Text = _Text
SelectionStart = 4
Case 6
_Text = Strings.Left(Text, 5)
_Text += "/" + Strings.Right(Text, 1)
Text = _Text
SelectionStart = 7
Case 10 'Sortie
SendKeys.SendWait("{TAB}")
End Select
End If
End Sub
Protected Overrides Sub OnGotFocus(e As EventArgs)
MyBase.OnGotFocus(e)
If ShowFocus = True Then
BackColor = FocusColor
End If
End Sub
Protected Overrides Sub OnLostFocus(e As EventArgs)
MyBase.OnLostFocus(e)
If ShowFocus = True Then
BackColor = Color.FromKnownColor(KnownColor.ButtonHighlight)
End If
End Sub
Protected Overrides Sub OnValidating(e As CancelEventArgs)
MyBase.OnValidating(e)
If Text <> "" And Type <> TextboxType.IsText Then
Dim Nb As String = NbDecimal.GetHashCode.ToString
Try
Select Case Type
Case TextboxType.IsMonnaie
Text = Format(CType(Text, Double), "C" & Nb).ToString
Case TextboxType.IsDate
Text = Format(CType(Text, Double), "d").ToString
Case TextboxType.IsNumeric
Text = Format(CType(Text, Double), "n" & Nb).ToString
End Select
Catch ex As Exception
Text = ""
Me.Select()
SetMainParent()
MetroMessageBox.Show(_parent, "Saisie numérique obligatoire...!", "Defaut de Format", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End If
End Sub |
Partager