How to close the program by pressing the

0

How can I close the program by pressing ESC? Even if it has a MessageBox or something else stopping it? I tried this way but could not:

 private void Form_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                Process.GetCurrentProcess().Kill();
            }
        }

Form:

 public partial class Home : Form
    {
        public Home()
        {
            InitializeComponent();
            MaximizeBox = false;
            statusID.Visible = false;
            this.KeyDown += Home_KeyDown; 
        }

        private void Home_FormClosing(object sender, FormClosingEventArgs e)
        {
            aplicacaoRodando = false;
        }

        bool aplicacaoRodando = true;
        private void materialFlatButton2_Click(object sender, EventArgs e)
        {
            txtID.Enabled = false;
            aplicacaoRodando = true;
            try
            {
                labelworking.Visible = true;
                labelworking.Text = "Em funcionamento..";
                btnscriptshutdown.Enabled = true;
                btnTurn.Enabled = false;
                Thread t = new Thread(() =>
                    {

                        while (aplicacaoRodando)
                        {
                            Thread.Sleep(1000);
                        }

                    });

                t.Start();
            }
            catch (Exception ex)
            {
            }
        }

        private void Home_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                nicon.BalloonTipTitle = "HOME";
                nicon.BalloonTipIcon = ToolTipIcon.Info;
                nicon.Icon = new Icon(Icon, 40, 40);
                nicon.Visible = true;
                this.Hide();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                nicon.Visible = false;
            }
        }


        private void notifyIcon1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Visible = true;
                WindowState = FormWindowState.Normal;
            }
        }

        private void cmssair_Click(object sender, EventArgs e)
        {
            aplicacaoRodando = false;
            Close();
        }
        private void Btnscriptshutdown_Click(object sender, EventArgs e)
        {
            txtID.Enabled = true;
            aplicacaoRodando = false;
            btnTurn.Enabled = true;
            btnscriptshutdown.Enabled = false;
            labelworking.Visible = false;
        }

        private void Home_KeyDown(object sender, KeyEventArgs e)
        {
            aplicacaoRodando = false;
            if (e.KeyCode == Keys.Escape)
            {
                System.Windows.Forms.Application.Exit();
            }
        }

        private void materialSingleLineTextField1_TextChanged(object sender, EventArgs e)
        {
            btnvalidar.Enabled = !(string.IsNullOrWhiteSpace(txtID.Text));
        }

        private void materialSingleLineTextField1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
            {
                statusID.Visible = true;
                statusID.Text = "Somente números";
                e.Handled = true;
            }
            else
            {
                statusID.Visible = false;
            }
        }
    }
}
    
asked by anonymous 28.05.2018 / 03:20

1 answer

3

Try this:

public Home()
{
    InitializeComponent();

    this.KeyDown += Home_KeyDown;
}

private void Home_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)
    {
        System.Windows.Forms.Application.Exit();
    }
}

From the MSDN :

Application.Exit ()

  

Reports all messages that should be finalized,   closes all windows of the application after processing the   messages.

    
28.05.2018 / 03:58