Search
VB
-
Tips
Visual Basic .Net tips and tricks
1
Previous
Next
DataGrid: Raise Event When User Presses Delete Key using OS messages
To be notified when the user presses the delete key to delete a row in a datagrid you have to create a new datagrid. You must override the PreprocessMessage and the ProcessDialogKey events in the datagrid. Here is a sample class that inherits from the datagrid that raises an event when a user deletes a row.
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
End Class
1
Previous
Next
Syndication
Rss feed
Select a Category
General Grid Tips
DataGrid Tips
DataGridView Tips
Windows Forms Controls
General Ado.Net Tips
SqlClient Ado.Net Tips
OleDB Ado.Net Tips
Asp.Net Tips
Miscellanous Tips
Vista programming Tips
WPF Tips
Silverlight
Articles
Links
Code Camp
FAQs
VB Related Websites
Microsoft Visual Basic Home Page
Visual Basic .Net Wikipedia Page
Visual Basic Forums
Visual Basic Newsgroup
VB City
Thinq Linq
VB Helper
I love VB