Show confirmation dialog when the DEL key pressed in 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;
    }




About TheScarms
About TheScarms


Sample code
version info

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

Email this page


© Copyright 2024 TheScarms
Goto top of page