How do I intercept the Delete key and show a confirmation message?

This is the simplest approach (WinForms) to show a dialog box to ask for confirmation before deleting selected entities:

public class MyDesign : Design
{ 
    protected override void OnKeyDown(KeyEventArgs e) 
    { 
        if (e.KeyCode == Keys.Delete) 
        { 
            if (MessageBox.Show("Do you want to delete the selected entities?", "Title", MessageBoxButtons.YesNo) == DialogResult.Yes) 
            {
                base.OnKeyDown(e);
            }
        } 
    } 
}
Was this article helpful?
4 out of 5 found this helpful

Comments

2 comments
Date Votes
  • Hi Alberto,

    After adding this code, when i press the No button it still deletes the entity. We need to add an else case.

    Regards

    Z.Mustafa Cakir

    0
  • I'm sorry. It's my mistake. Code works perfect.

     

    Best regards

    Z.Mustafa Cakir

    0

Please sign in to leave a comment.

Articles in this section

See more