When I try this (having fixed your code so it compiles):
DialogResult result = MessageBox.Show(this, "Please confirm", "Are you sure to exit the application", MessageBoxButtons.YesNo);
Or
DialogResult result = MessageBox.Show("Please confirm", "Are you sure to exit the application", MessageBoxButtons.YesNo);
The result is the same: you can't press the "X" button to close the dialog, it's greyed out.
And trying to click the "X" button on the main form doesn't work either - because the dialog box is modal and the main form won't respond until it's closed.
So what exactly are you doing differently - because I suspect your code doesn't look like what you showed us...
"I think he is clicking on the taskbar icon that offers tiny red X to close the app "from outside""
"yes"
AH!
In which case, it's simple: handle the FormClosing event and show the message box from that: you can cancel the close via the FormClosingEventArgs.Cancel property
"
I have tried that. But, issue is still exist.
In 'Cancel' button event, i have added FormClosingEventArgs.Cancel= false;
Then In Form Closing event, added the Message box to display.
When i clicked on cancel button, message box displayed. Then again i have clicked on task bar icon close window, focus is moving from message box to Main Window.
"
I tried it myself by modding an existing app:
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Please confirm", "Are you sure to exit the application", MessageBoxButtons.YesNo);
if (result != DialogResult.Yes)
{
e.Cancel = true;
return;
}
if ((ModifierKeys & Keys.Shift) == 0)
{
this.SaveLocation();
}
}
I run the app, hover the mouse over the taskbar and click "X" - the message box appears.
If I then hover the mouse over the taskbar again, I get two preview windows and I can switch the focus to the "main" form from the dialog box but I can't do anything with it - All I get is a default beep if I try.
This is a "feature" of windows I suspect, and there is no real way to get rid of it.
So...You will have to work round it.
Bear with me here...
Start by adding a new form to your project - call it MyMessageBox
Set the caption, add a label to prompt the user to close it, and add two buttons: call one "butYes", the other "butNo" and set the text appropriately. Set the DialogResult property for each button to "Yes" and "No" respectively.
Set the form properties "AcceptButton" to "butYes", and "CancelButton" to "butNo".
Set the form properties "ShowInTaskBar" to "false", and "StartPosition" to "CenterParent".
Now, back to your main form, and add two private class variables:
private bool checkClosing = false;
private MyMessageBox closeConfirm = null;
Change your FormClosing event handler:
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
checkClosing = true;
closeConfirm = new MyMessageBox();
DialogResult result = closeConfirm.ShowDialog();
checkClosing = false;
closeConfirm = null;
if (result != DialogResult.Yes)
{
e.Cancel = true;
return;
}
}
(Technically, you only need the closeConfirm variable, but...)
Now add a new form event handler: Activated
private void frmMain_Activated(object sender, EventArgs e)
{
if (checkClosing && closeConfirm != null)
{
closeConfirm.Activate();
}
}
Compile and try it.