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;
}
}
}
}
I don’t know why but nothing happens, but by button, using the Close() method; it closes normally.
– user92401
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?
– perozzo
Only one, the name is Home
– user92401
I updated the answer. Can you test? In the example I use Home as the name of your Form, based on what you said.
– perozzo
Nothing happened either
– user92401
Edit your question and enter the entire code of your Form, please.
– perozzo
Modified ready
– user92401
Let’s go continue this discussion in chat.
– perozzo
Get in the chat room so we can sort it out!
– perozzo