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
| Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Dim voControl As DataGridViewTextBoxEditingControl
Try
If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name = "DataGridViewTextBoxColumnEpmm" Then
voControl = CType(e.Control, DataGridViewTextBoxEditingControl)
AddHandler voControl.KeyPress, AddressOf EditingControl_KeyPress
End If
Catch ex As Exception
End Try
'Obtenir le composant
Dim grid As DataGridView = DirectCast(sender, DataGridView)
'Définir le nom des colonnes (à changer en fonction de tes propres paramètres)
Dim colonneQuantite As String = "QuantitéDataGridViewTextBoxColumn"
Dim colonnePrixUnit As String = "PrixUDataGridViewTextBoxColumn"
Dim colonneTotal As String = "TotalDataGridViewTextBoxColumn"
'Traitement en fonction de la colonne où se situe la cellule en cours d'édition
Dim indexColonne As Integer = grid.CurrentCell.ColumnIndex
Select Case grid.Columns(indexColonne).Name
Case colonnePrixUnit
'Récupération du contrôle contenu (Textbox)
Dim txtDecimal As TextBox = DirectCast(e.Control, TextBox)
'Envoi vers la routine de traitement d'un nombre décimal
AddHandler txtDecimal.KeyPress, AddressOf txtDecimal_KeyPress
Case colonneQuantite
'Récupération du contrôle contenu (Textbox)
Dim txtEntier As TextBox = DirectCast(e.Control, TextBox)
'Envoi vers la routine de traitement d'un nom entier
AddHandler txtEntier.KeyPress, AddressOf txtEntier_KeyPress
End Select
End Sub
Private Sub txtDecimal_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
Dim txtDecimal As TextBox = DirectCast(sender, TextBox)
Dim separateur As String = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator
If "0123456789".Contains(e.KeyChar) = True Then
'Les chiffres sont acceptés
Exit Sub
ElseIf ",.".Contains(e.KeyChar) = True Then
'Traitement de la virgule (, ou .)
If txtDecimal.Text.Contains(separateur) = False Then
'On modifie la touche en séparateur décimal courant
e.KeyChar = separateur
Else
'Il y a déjà une virgule, on "lève" l'évènement
e.Handled = False
End If
ElseIf e.KeyChar <> Convert.ToChar(Keys.Back) Then
'On accepte le retour arrière. Le reste des touches est inhibé (e.handled=true)
e.Handled = True
End If
End Sub
Private Sub txtEntier_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If "0123456789".Contains(e.KeyChar) = True Then
'Les chiffres sont acceptés
Exit Sub
ElseIf e.KeyChar <> Convert.ToChar(Keys.Back) Then
'On accepte le retour arrière. Le reste des touches est inhibé (e.handled=true)
e.Handled = True
End If
End Sub |
Partager