Prevent Deletes when the DELete key is pressed in a DataGrid
You can do this by subclassing your DataGrid and
overriding the ProcessDialogKey and
PreProcessMessage methods to trap for the DEL key. I also add a new
AllowDelete property to the DataGrid so you can toggle this
functionality on and off.
Add this module level variable:
' Allow delete KEY flag.
Private myAllowDelete As Boolean = False
Add this property:
Public Property AllowDelete() As Boolean
'
' Get/set property indicating if rows can be deleted.
'
Get
AllowDelete = myAllowDelete
End Get
Set(ByVal theValue As Boolean)
myAllowDelete = theValue
End Set
End Property
Add these methods:
Public Overrides Function PreProcessMessage(_
ByRef msg As System.Windows.Forms.Message) As Boolean
'
' Allow/disallow deletes when the DEL key is pressed.
'
On Error Resume Next
Dim keyCode As Keys =
CType((msg.WParam.ToInt32 And Keys.KeyCode), Keys)
If keyCode = Keys.Delete And msg.Msg = WM_KEYDOWN Then
If NOT myAllowDelete Then
Return True
End If
End If
Return MyBase.PreProcessMessage(msg)
End Function
Protected Overrides Function ProcessDialogKey( _
ByVal keyData As System.Windows.Forms.Keys) As Boolean
'
' Allow/disallow deletes when the DEL key is pressed.
'
On Error Resume Next
If keyData = Keys.Delete Then
If myHitTestInfo.Type = Me.HitTestType.RowHeader Then
If NOT myAllowDelete Then
Return True
End If
End If
End If
End Function
|
About TheScarms
Sample code version info
|