Search Amazon:

Show a confirmation dialog when the DEL key is hit in a DataGrid

You can do this by subclassing your DataGrid and overriding the ProcessDialogKey and PreProcessMessage methods. I also add a new ConfirmDelete property to the DataGrid so you can toggle confirmations on and off.

VB.NET code:

Add this module level variable:

    ' Confirm deletes flag.
    Private myConfirmDelete As Boolean = True

Add this property:

    Public Property ConfirmDelete() As Boolean
        '
        ' Get/set property indicating if rows deletes must be confirmed.
        '
        Get
            ConfirmDelete = myConfirmDelete
        End Get

        Set(ByVal theValue As Boolean)
            myConfirmDelete = theValue
        End Set
    End Property

Add these methods:

    Public Overrides Function PreProcessMessage( _
          ByRef msg As System.Windows.Forms.Message) As Boolean
        '
        ' Disallow and/or Confirm row 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 myConfirmDelete Then
                If MsgBox("Delete this row?", _
                           MsgBoxStyle.Question + MsgBoxStyle.YesNo + _
                           MsgBoxStyle.DefaultButton1, _
                           "Confirm Delete") = MsgBoxResult.No Then

                    Return True
                End If
            End If
        End If

        Return MyBase.PreProcessMessage(msg)

    End Function

    Protected Overrides Function ProcessDialogKey( _
         ByVal keyData As System.Windows.Forms.Keys) As Boolean
        '
        ' Disallow and/or Confirm row deletes when the DEL key is pressed.
        '
        On Error Resume Next

        If keyData = Keys.Delete Then
            If myHitTestInfo.Type = Me.HitTestType.RowHeader Then
                If myConfirmDelete Then
                    If MsgBox("Delete this row?", _
                               MsgBoxStyle.Question + MsgBoxStyle.YesNo + _
                               MsgBoxStyle.DefaultButton1, _
                               "Confirm Delete") = MsgBoxResult.No Then

                        Return True
                    End If
                End If
            End If
        End If
    End Function

C# code:

Add this field:

    // Confirm deletes flag.
    private bool myConfirmDelete = true;

Add this property:

    public bool ConfirmDelete
    {
        //
        // Get/set property indicating if rows deletes must be confirmed.
        //
        get
        {
            return myConfirmDelete;
        }
        set
        {
        myConfirmDelete = value;
        }
    }

Add these methods:

    public override bool PreProcessMessage(ref System.Windows.Forms.Message msg)
    {
        //
        // Disallow and/or Confirm row deletes when the DEL key is pressed.
        //
        Keys keyCode = (Keys)(msg.WParam.ToInt32() & (int)Keys.KeyCode);

        if ((keyCode == Keys.Delete) & (msg.Msg == WM_KEYDOWN)) 
        {
            if (myConfirmDelete)
            {
                if (MessageBox.Show("Delete this row?", "Confirm Delete", 
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question, 
                    MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    return true;
                }
            }
        }
        return base.PreProcessMessage(ref msg);
    } // PreProcessMessage


    protected override bool ProcessDialogKey(System.Windows.Forms.Keys keyData)
    {
        //
        // Confirm row deletes when the DEL key is pressed.
        //
        if (keyData == Keys.Delete)
        {
            if (myHitTestInfo.Type == HitTestType.RowHeader)
            {
                if (myConfirmDelete)
                {
                    if (MessageBox.Show("Delete this row?", "Confirm Delete", 
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question, 
                        MessageBoxDefaultButton.Button2) == DialogResult.No)
				
                        return true;
                    }
                }
            }
        }
        return false;
    }

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