Default Event Exit on the ESC key

2

Currently on my system I use the KeyPress event to identify the key Esc and thus close my form, however I have a large system with more than 50 forms and even more being developed: / p>

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

    
asked by anonymous 10.04.2017 / 15:17

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 forms and whenever you create a new form, make this new 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 this for all forms , including those that already exist, you can simply create a class called Form . Note that all current forms already inherit from a class called Form , so from the moment you define a class with this name, the current forms will inherit the form that was created within your project.

Then, the default%% would look like this

public class Form : System.Windows.Forms.Form { }
    
10.04.2017 / 15:22