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

Here is the simplest approach to show a dialog box to ask a confirmation before deleting selected entities:

public class MyVieportLayout : ViewportLayout 
{ 
    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?
3 out of 3 found this helpful
Have more questions? Submit a request

Comments

2 comments
  • 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

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

     

    Best regards

    Z.Mustafa Cakir

Please sign in to leave a comment.