The first thing to note is that your indentation is wrong - which if you are using Visual Studio probably means that your code doesn't compile. If it does, use CTRL+K, D to format the document properly so you can see what is going on:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (ShowDataInScreenTxtb.Text == string.Empty)
{
DialogResult dialogResultExit = MessageBox.Show("Do you really want to Close the Application?",
"Close Alert!",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (dialogResultExit == DialogResult.Yes)
{
return;
}
else if (dialogResultExit == DialogResult.No)
{
e.Cancel = true;
}
}
if (ShowDataInScreenTxtb.Text != string.Empty)
{
DialogResult dialogForClose = MessageBox.Show("Data displayed on screen will be deleted!\n\n" +
"Would you like to Save it before Closing the application?",
"Save Data Alert",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Warning);
switch (dialogForClose)
{
case DialogResult.Yes:
this.SaveToFile();
break;
case DialogResult.No:
this.saveChanges = false;
break;
}
e.Cancel = this.saveChanges;
}
}
The first thing to note is that
this.saveChanges
is only set in one branch - when dialogForClose is "No" you set it to false - so it's value is unknown / indeterminate when the value is "Yes". Setting it to true when the result is "Yes" may solve your problem.