Close Button
-
I would like to know how to display a message box with an "Are you sure you want to exit?" when someone clicks the close button, therefore the cross button. I managed to do it in a simple button but I think it's different in the close button! Can someone help me please?! Thanks ;) Alison
-
I would like to know how to display a message box with an "Are you sure you want to exit?" when someone clicks the close button, therefore the cross button. I managed to do it in a simple button but I think it's different in the close button! Can someone help me please?! Thanks ;) Alison
-
I would like to know how to display a message box with an "Are you sure you want to exit?" when someone clicks the close button, therefore the cross button. I managed to do it in a simple button but I think it's different in the close button! Can someone help me please?! Thanks ;) Alison
Override the
OnClosing
event of your form:this.FormClosing += new FormClosingEventHandler(OnClosing);
and use this code:
private void OnClosing(object sender, FormClosingEventArgs e)
{
DialogResult res = MessageBox.Show("Are you sure you want to close the form?",
"Question",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question);
if (res != DialogResult.Yes)
{
e.Cancel = true; // do not close this form
}
}regards -- modified at 10:41 Monday 20th February, 2006
-
Override the
OnClosing
event of your form:this.FormClosing += new FormClosingEventHandler(OnClosing);
and use this code:
private void OnClosing(object sender, FormClosingEventArgs e)
{
DialogResult res = MessageBox.Show("Are you sure you want to close the form?",
"Question",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question);
if (res != DialogResult.Yes)
{
e.Cancel = true; // do not close this form
}
}regards -- modified at 10:41 Monday 20th February, 2006
Greg, that isn't overriding, that is event subscription. To override, you don't need an event handler:
protected override void OnClosing(CancelEventArgs e)
{
DialogResult res = MessageBox.Show("Are you sure you want to close the form?",
"Question",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question);
if (res != DialogResult.Yes)
{
e.Cancel = true; // do not close this form
}
} -
Override the
OnClosing
event of your form:this.FormClosing += new FormClosingEventHandler(OnClosing);
and use this code:
private void OnClosing(object sender, FormClosingEventArgs e)
{
DialogResult res = MessageBox.Show("Are you sure you want to close the form?",
"Question",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question);
if (res != DialogResult.Yes)
{
e.Cancel = true; // do not close this form
}
}regards -- modified at 10:41 Monday 20th February, 2006
[Message Deleted]
-
Greg, that isn't overriding, that is event subscription. To override, you don't need an event handler:
protected override void OnClosing(CancelEventArgs e)
{
DialogResult res = MessageBox.Show("Are you sure you want to close the form?",
"Question",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question);
if (res != DialogResult.Yes)
{
e.Cancel = true; // do not close this form
}
} -
[Message Deleted]
-
this.FormClosing += new FormClosingEventHandler(OnClosing); This should be put into the form constructor
Ohh I made it, Thanks a lot people for helping me! :-D