Search Amazon:

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
Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms