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
| Private Sub LongueurTexteDgGrille(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DGrille.EditingControlShowing
Dim Cellule As DataGridViewTextBoxEditingControl
Try
'limite la longueur de char dans les Cellule
Select Case DGrille.Columns(DGrille.CurrentCell.ColumnIndex).Name
Case "NumArticle"
Try
Cellule = CType(e.Control, DataGridViewTextBoxEditingControl)
Cellule.MaxLength = 25 'longueur du texte max
Catch ex As Exception
End Try
Case "Quantite"
Try
Cellule = CType(e.Control, DataGridViewTextBoxEditingControl)
Cellule.MaxLength = 10 'longueur du texte max
Catch ex As Exception
End Try
Case "PrixUnitaire"
Try
Cellule = CType(e.Control, DataGridViewTextBoxEditingControl)
Cellule.MaxLength = 10 'longueur du texte max
Catch ex As Exception
End Try
Case "RemiseLigne"
Try
Cellule = CType(e.Control, DataGridViewTextBoxEditingControl)
Cellule.MaxLength = 4 'longueur du texte max
Catch ex As Exception
End Try
End Select
Catch
End Try
End Sub
Private Sub dgListe_Separateur(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DGrille.EditingControlShowing
Dim voControl As DataGridViewTextBoxEditingControl = Nothing
'je récupère le control TextBox de la cellule qui est édité
voControl = CType(e.Control, DataGridViewTextBoxEditingControl)
'Je recupere le Handles de KeyDown sur toutes les colonnes
RemoveHandler voControl.KeyDown, AddressOf EditingControl_KeyDown
AddHandler voControl.KeyDown, AddressOf EditingControl_KeyDown
'si la colonne qui est éditée est une de nombres alors on gère l'événement KeyPress du TextBox
Select Case Me.DGrille.Columns(DGrille.CurrentCell.ColumnIndex).Name
Case "Quantite", "PrixUnitaire", "RemiseLigne"
RemoveHandler voControl.KeyPress, AddressOf EditingControl_KeyPress
AddHandler voControl.KeyPress, AddressOf EditingControl_KeyPress
Case Else
RemoveHandler voControl.KeyPress, AddressOf EditingControl_KeyPress
End Select
End Sub
Private Sub EditingControl_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
'Calcul la tva inverse sur la ligne en cours
If e.KeyCode = Keys.F3 Then CalCulInverseTva(DGrille.CurrentRow.Index)
'Supprime la ligne en cours dans le datagridview
If e.KeyCode = Keys.F5 Then DGrille.Rows.Remove(DGrille.CurrentRow)
End Sub
Private Sub EditingControl_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim vsDecimalSeparator As Char
'j'accepte que les caractères numériques, le point, ou la virgule
e.Handled = Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or e.KeyChar = "." Or e.KeyChar = ",")
'je récupère le texte du TextBox
Dim txt As String = CType(sender, DataGridViewTextBoxEditingControl).Text
'je regarde que le point ou la virgule n'a été tapé qu'une fois
If (InStr(txt, ".") > 0 Or InStr(txt, ",") > 0) And (e.KeyChar = "." Or e.KeyChar = ",") Then
e.KeyChar = Nothing
Else
'je remplace le point par une virgule en fonction du séparateur décimal utilisé dans la culture en cours
vsDecimalSeparator = CChar(System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator)
If vsDecimalSeparator <> "." And e.KeyChar = "." Then
e.KeyChar = vsDecimalSeparator
End If
End If
End Sub |
Partager