Exit Standard Event on ESC key

Asked

Viewed 265 times

2

Currently in my system I use the event KeyPress to identify the key Esc and so close my form, but I have a very large system with more than 50 forms and even more being developed:

My question is: Is there any standard way to set the key Esc as Exit form?

  • 1

    Create a standard form that contains this functionality and all others that are required and inherit all other forms from this.

  • In case this would be feasible only for the new correct Foms?

  • 1

    @Leolonghi No, if you make the name of this form (class) to be Form, This will be worth for those who have already been created too.

  • @Some way to undo this?

  • @Leolonghi yes, just click "restore" below it. But it was just a suggestion, see how much better you think. No problem have deleted, it was just an idea.

1 answer

4


Yes, there are several ways to do this. My tip for you is to create a simple form with all the default behaviors for the system’s Forms and whenever you create a new form, make this new one inherit the default form.

public class FormPadrao : Form
{
    /* Outros métodos */

    private void form_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
        {
            this.Close();
        }
    }
}

And the use would look like this

public FormCadastro : FormPadrao { }

If you need it for all the Forms, including those that already exist can simply create a class called Form. Realize that all the Forms already inherited from a class called Form, then, from the moment you define a class with this name, the current Forms will inherit the form that was created within your project.

So the Form pattern would look like this

public class Form : System.Windows.Forms.Form { }
  • Thank you, that’s what I needed! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.