?
Solved

Datagridview keypress

Posted on 2007-09-30
5
Medium Priority
?
3,478 Views
Last Modified: 2012-08-13
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


       
0
Comment
Question by:thesmashest
  • 4
5 Comments
 
LVL 18

Expert Comment

by:vbturbo
ID: 19989081
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
0
 
LVL 18

Expert Comment

by:vbturbo
ID: 19990098
or

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

vbturbo
0
 

Author Comment

by:thesmashest
ID: 19991479
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.
0
 
LVL 18

Accepted Solution

by:
vbturbo earned 200 total points
ID: 19999573
Well this works fine by me in vb.net 2005

    Private Sub dtGrid_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtGrid.KeyUp
        If e.Control And e.KeyCode = Keys.C Then
            MessageBox.Show("Working")
        End If
    End Sub

vbturbo

0
 
LVL 18

Expert Comment

by:vbturbo
ID: 20000630
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question