473,322 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to trigger the keydown event in datagrid control ?

I would like to use datagrid's keydown event to capture the "Ctrl+D" when
users press this key. But it did not work. What did I miss? (I have already
set the form keypreview to true)

Private Sub grdInCategory_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles grdInCategory.KeyDown
If e.Control And e.KeyCode = Keys.D Then
If MsgBox("Are you sure to delete the record ?",
MsgBoxStyle.YesNo) = MsgBoxResult.No Then Exit Sub
If (Me.BindingContext(Me.objdsInCategory, "InCategory").Count >
0) Then
Me.BindingContext(Me.objdsInCategory,
"InCategory").RemoveAt(Me.BindingContext(Me.objdsI nCategory,
"InCategory").Position)
End If
End If
End Sub

Thank you
John Huang
Nov 21 '05 #1
2 6109
John,

Everything you always wanted to know from the datagrid
http://64.78.52.104/FAQ/WinForms/default.asp#44

I hope this helps,

Cor
Nov 21 '05 #2
Hi,

You need to create an inherited datagrid for that. Here is the code
for one that will raise an event to let you the user is about to delete a
row. Also prevents the user from resizing a row or column.

Public Class ConfirmDeleteDataGrid

Inherits DataGrid

Public Event DeletedRow(ByVal sender As Object, ByVal e As EventArgs)

Private Const WM_KEYDOWN = &H100

Public Overrides Function PreProcessMessage(ByRef msg As
System.Windows.Forms.Message) As Boolean

Dim keyCode As Keys = CType((msg.WParam.ToInt32 And Keys.KeyCode), Keys)

If msg.Msg = WM_KEYDOWN And keyCode = Keys.Delete Then

If MessageBox.Show("Delete This Row?", "Confirm Delete", _

MessageBoxButtons.YesNo) = DialogResult.No Then

Return True

Else

RaiseEvent DeletedRow(Me, New EventArgs)

End If

End If

Return MyBase.PreProcessMessage(msg)

End Function

Protected Overrides Function ProcessDialogKey(ByVal keyData As
System.Windows.Forms.Keys) As Boolean

Dim pt As Point

Dim hti As DataGrid.HitTestInfo

pt = Me.PointToClient(Cursor.Position)

hti = Me.HitTest(pt)

If keyData = Keys.Delete Then

If hti.Type = Me.HitTestType.RowHeader Then

If MessageBox.Show("Delete this row?", "Confirm Delete", _

MessageBoxButtons.YesNo) = DialogResult.No Then

Return True

Else

RaiseEvent DeletedRow(Me, New EventArgs)

End If

End If

End If

Return MyBase.ProcessDialogKey(keyData)

End Function

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)

Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X, e.Y))

If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type =
DataGrid.HitTestType.RowResize Then

Return 'no baseclass call

End If

MyBase.OnMouseDown(e)

End Sub

Public Sub New()

Trace.WriteLine(Me.VertScrollBar.Visible.ToString)

End Sub

Protected Overrides Sub OnMouseMove(ByVal e As
System.Windows.Forms.MouseEventArgs)

Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X, e.Y))

If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type =
DataGrid.HitTestType.RowResize Then

Return 'no baseclass call

End If

MyBase.OnMouseMove(e)

End Sub
End Class

Ken

-------------------------------
"John Huang" <John Hu***@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
I would like to use datagrid's keydown event to capture the "Ctrl+D" when
users press this key. But it did not work. What did I miss? (I have already
set the form keypreview to true)

Private Sub grdInCategory_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles grdInCategory.KeyDown
If e.Control And e.KeyCode = Keys.D Then
If MsgBox("Are you sure to delete the record ?",
MsgBoxStyle.YesNo) = MsgBoxResult.No Then Exit Sub
If (Me.BindingContext(Me.objdsInCategory, "InCategory").Count >
0) Then
Me.BindingContext(Me.objdsInCategory,
"InCategory").RemoveAt(Me.BindingContext(Me.objdsI nCategory,
"InCategory").Position)
End If
End If
End Sub

Thank you
John Huang
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fred Heida | last post by:
Hi All, maybe this is a sill question..but if i have TextBox and use the SendMessage(..) to send a character the control.. the KeyDown event is not triggered.... in would need to check in the...
4
by: Mike M | last post by:
I am trying to detect the keydown of the arrow keys in a custom control that I am writing. As far as I can tell, pressing the arrow keys does not fire the KeyDown event at all. It behaves just...
3
by: bardo | last post by:
I have a Datagrid that is inside a panel. I want to use the keyDown event to reconize the arrow keys. But I have no luck at all. The problem is that the keydown event won't fire at all, unless I...
4
by: Tony | last post by:
I'm building a simple "breakout" style video-game, and I want keyboard control of the paddle. What I need is for an event handler to respond whenever a key is pressed, regardless of what the...
2
by: Rolf Falnes | last post by:
Hi. Does anyone know how to trigger the doubleclick mouse event in a datagrid cell (DatagridTextbox )? Regards, Rolf Falnes Datapartner AS
0
by: Peter | last post by:
I have a VB6 program which can receive Keydown events on an ActiveX control. The ActiveX control can't fire keydown events so I put a picturebox below the ActiveX control. I write codes in...
1
by: fripper | last post by:
I have a VB 2005 windows app and I want to recognize keydown events. I have a form key down event handler but it does not get control when a key is depressed. In playing around I found that if I...
3
by: MLM450 | last post by:
I have a control that handles the KeyDown event but it does not seem to execute when a combination of keys is pressed - like CTRL+Z. If I press CTRL, it executes. If I press Z, it executes. But the...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.