Как предотвратить или заблокировать закрытие окна WinForms?
Как предотвратить закрытие окна, показывая MessageBox
? (Технология: WinForms
с C#
)
Когда произойдет событие закрытия, я хочу запустить следующий код:
private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if (closeMsg == DialogResult.Yes) {
//close addFile form
} else {
//ignore closing event
}
}
Ответы
Ответ 1
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
var window = MessageBox.Show(
"Close the window?",
"Are you sure?",
MessageBoxButtons.YesNo);
e.Cancel = (window == DialogResult.No);
}
Ответ 2
Событие Catch FormClosing и установите e.Cancel = true
private void AdminFrame_FormClosing(object sender, FormClosingEventArgs e)
{
var res = MessageBox.Show(this, "You really want to quit?", "Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
if (res != DialogResult.Yes)
{
e.Cancel = true;
return;
}
}
Ответ 3
В вашем OnFormClosing
событии вы можете показать диалог, а if
ответ будет ложным (чтобы не показать), а затем установите для свойства Cancel
значение EventArgs
на true
.
Ответ 4
Особым поводом может быть всегда предотвращение закрытия пользователем формы:
private void Frm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = (e.CloseReason == CloseReason.UserClosing);
// disable user closing the form, but no one else
}
Ответ 5
Чтобы предотвратить или заблокировать закрытие формы в конкретной ситуации, вы можете использовать эту стратегию:
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (FLAG_CONDITION == true)
{
MessageBox.Show("To exit save the change!!");
e.Cancel = true;
}
}
Ответ 6
Прямо от MSDN:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Determine if text has changed in the textbox by comparing to original text.
if (textBox1.Text != strMyOriginalText)
{
// Display a MsgBox asking the user to save changes or abort.
if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Cancel the Closing event from closing the form.
e.Cancel = true;
// Call method to save file...
}
}
}
В вашем случае вам не нужно ничего делать, чтобы явно закрыть форму. Если вы не отмените его, он автоматически закроется, поэтому ваш код будет выглядеть следующим образом:
private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if (closeMsg == DialogResult.Yes) {
// do nothing
} else {
e.Cancel = true;
}
}
Ответ 7
private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if (closeMsg == DialogResult.Yes) {
e.Cancel = false;
} else {
e.Cancel = true;
}
e.Cancel - включить или отключить событие закрытия формы. Например, e.Cancel = true
отключит ваше закрытие.