-
May 24th, 2010, 02:29 AM
#1
Thread Starter
Addicted Member
How do I create an On-KeyDown event for a datagridview cell?
I'm using an on key down event for my datagridviewer that fires every time a key is pressed while the datagridviewer is selected(Focused).
I want the same thing to happen while in a cell, but there is no event for key down in a cell.
How do I code one?
-
May 24th, 2010, 02:42 AM
#2
Re: How do I create an On-KeyDown event for a datagridview cell?
The way the DataGridView works is that the contents of each cell is simply drawn on until you actually start editing a cell. At that point a control of the appropriate type is created if one doesn't already exists and it is embedded in the cell. At that point, you have to handle events of that control rather than of the grid. To do that, you handle the EditingControlShowing event of the grid, get a reference to the editing control and use AddHandler to attach its event(s) to the appropriate handler(s).
One important point is that, if that's all you do, you'll end up handling the same event multiple times because you'll keep adding the same handler over and over. For that reason, you should precede AddHandler with RemoveHandler. That will have no effect if there's no existing handler, otherwise it will remove the existing handler before adding the new one. E.g.
vb.net Code:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, _ ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing 'Make sure we are in the first column. If Me.DataGridView1.CurrentCellAddress.X = 0 Then With e.Control 'Remove the existing handler if there is one. RemoveHandler .TextChanged, AddressOf TextBox_TextChanged 'Add a new handler. AddHandler .TextChanged, AddressOf TextBox_TextChanged End With End If End Sub Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) '... End Sub
-
May 24th, 2010, 02:51 AM
#3
Thread Starter
Addicted Member
Re: How do I create an On-KeyDown event for a datagridview cell?
Oky I see where youre going with this. I have a certain code that works for my dgv-keyDown. How do I implement it in the code you posted above?
vb Code:
Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown Select Case e.KeyCode Case Keys.Return e.SuppressKeyPress = True SendKeys.Send("{TAB}") End Select End Sub
-
May 24th, 2010, 07:25 PM
#4
Re: How do I create an On-KeyDown event for a datagridview cell?
So what efforts have you made so far? I've shown you how to get a reference to the editing control and how to add a handler for an event of that control. All you have to do is pick the appropriate event and write a handler for it. You've already got an event handler because you wrote one for the griod, so you don't even have to do that part.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|