My Winforms app has a dialog that opens on first run to capture a SQL connection string. The dialog is triggered by a MessageBox and the capture form shows when the user clicks "OK". What I'd like is the application to exit when the Cancel button is clicked however it's not working the way I'd expect.
while (GlobalSettings.getSqlConnection() == "")
{
var result = MessageBox.Show("A connection to the database is required in order for the application to function!\r\nPlease enter valid connection string(s) in the next window", "Error", MessageBoxButtons.OKCancel);
if (result.ToString() == "OK")
{
using (ConnectionStrings box = new ConnectionStrings())
{
box.ShowDialog(this);
}
}
else
{
Application.Exit();
}
}
// more code
I expect the application to close if the Cancel button is clicked however the "While" loop continues and the MessageBox shows again.
I realise I can create an "if" statement after this "While" loop and close from there but I'd prefer to handle it from the dialog result.