I know this code can exit my application
System.Windows.Forms.Application.Exit();
But how to apply this code for all my form Let say I have a lot of form Do I have to put this code in every form?
I know this code can exit my application
System.Windows.Forms.Application.Exit();
But how to apply this code for all my form Let say I have a lot of form Do I have to put this code in every form?
Wrap this line inside a static
method inside the class
containing the Main()
....that makes it somewhat modular, not much useful though.
You can try to use
this.close();
to close your currently active form. Similarly you can use it every form you want to close.
Refered to my comment you can call the Close
-method and use FormClosing
-event to close the other forms:
public partial class Form1 : Form
{
Form2 form2;
public Form1()
{
InitializeComponent();
FormClosing += Form1_FormClosing;
form2 = new Form2();
form2.Show();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
form2.Close();
}
private void button1_Click(object sender, EventArgs e)
{
Close();
}
}
In this example Form1 is your MainForm and if you close Form1 whether you use the Close
-method or by closing the form by the default button the FormClosing
event will be raised and all other forms will be closed too.
If you want to kill the current Process you can use this code:
Process.GetCurrentProcess().Kill();
In many windows forms projects I've worked we had a base form and all forms in our code inherited from this base form, so you can have a form like below and you just change the others forms to inherit from this one.
public partial class frmBase: Form
{
public frmBase()
{
InitializeComponent();
}
private void frmBase_FormClosed(object sender, FormClosedEventArgs e)
{
System.Windows.Forms.Application.Exit();
}
}