private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (App.Terminating || Application.OpenForms.Count > 1 || !(e.Cancel = !App.Close())) { ed.DetachBuffer(); WriteState(); } }