How to close the program by pressing the button

Asked

Viewed 614 times

0

How can I close the program by pressing the ESC key? Even if I have a Messagebox or something else preventing it? I tried this way but I couldn’t:

 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;
            }
        }
    }
}

1 answer

3


Try it this way:

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();
    }
}

Of MSDN:

Application Exit.()

It informs all messages that must be finalised and then closes all application windows after processing the messages.

  • I don’t know why but nothing happens, but by button, using the Close() method; it closes normally.

  • And are you in the right Form? How many Forms does your program have? What is the name of the Form that is trying to execute the above code?

  • Only one, the name is Home

  • I updated the answer. Can you test? In the example I use Home as the name of your Form, based on what you said.

  • Nothing happened either

  • Edit your question and enter the entire code of your Form, please.

  • Modified ready

  • Get in the chat room so we can sort it out!

Show 4 more comments

Browser other questions tagged

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