Link to home
Create AccountLog in
Avatar of thesmashest
thesmashest

asked on

Datagridview keypress

I have a datagridview, and I have Keypress Sub.
I want to add my own code whenever the user pressed Ctrl+C or Ctrl+V or Ctrl+X

here is what I tried, but it didn't work...any help?

Private Sub dtGrid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dtGrid.KeyPress
        If Keys.Control.ToStrting() AND e.KeyChar = "c" Then
            'Copy the current cell content
        End If
End Sub


       
Avatar of vbturbo
vbturbo
Flag of Denmark image

Try this

Private Sub dtGrid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dtGrid.KeyPress

   If Control.ModifierKeys = Keys.Control AND e.KeyChar = "c"  Then
      MessageBox.Show("Working")
   End If

End Sub

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.modifierkeys.aspx

vbturbo
or

If e.Control And e.KeyCode = Keys.C Then
MessageBox.Show("Working")
End If

vbturbo
Avatar of thesmashest
thesmashest

ASKER

I tried the first suggestion, but it didn't work. If I write it such as this, then it works.
   If e.KeyChar = "c"  Then
      MessageBox.Show("Working")
   End If

it looks like the Control.ModifierKeys = Keys.Control is not working. I even tried Control.ModifierKeys = Keys.ControlKey.

Your second suggestion doesn't work.
ASKER CERTIFIED SOLUTION
Avatar of vbturbo
vbturbo
Flag of Denmark image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
And for the keypress event

    Private Sub dtGrid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dtGrid.KeyPress
        Dim KeyAscii As Short = Asc(e.KeyChar)

        If Control.ModifierKeys = Keys.Control Then
            If KeyAscii = 3 Then
                MessageBox.Show(" Ctrl+C was trapped")
                e.Handled = True
            ElseIf KeyAscii = 22 Then
                MessageBox.Show(" Ctrl+V was trapped")
                e.Handled = True
            ElseIf KeyAscii = 24 Then
                MessageBox.Show(" Ctrl+X was trapped")
                e.Handled = True
            Else
                e.Handled = False
            End If
        End If

    End Sub