I have a simple application:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Constructor of Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// ...
if (some_condition)
{
DialogResult dr = MessageBox.Show("Do you want to continue ?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
if (dr == DialogResult.No)
{
// How to close the window normally or how to not create a Form1 instance ?
//
//
}
}
// amount of code that executes only if some_condtion == false
}
}
I know that I can check some_condition before Application.Run but it's difficult to change (believe me). I need to check some_condition in constructor of Form1. If some_condition == true and answer is no --> application closes.