[英]How can I show message boxes depending on a bool result?
我的Quit方法中目前有以下代碼:
public void QuitApplication(FormClosingEventArgs a, bool b)
{
bool c = Properties.App.Default.AskToExit, d = Properties.App.Default.AskToSave;
if (!b)
{
if (!c && !d)
{
}
else if (!c)
{
if (YesNoMessageBox("Save Before Quit?", "Would you like to save your settings before you Quit?") == DialogResult.Yes)
{
_debug("Settings Saved");
//Properties.App.Default.Save();
//Properties.Settings.Default.Save();
}
}
else if (!d)
{
if (YesNoMessageBox("Really Quit?", "Are you sure you want to quit?") == DialogResult.No)
{
a.Cancel = true;
}
}
else
{
if (YesNoMessageBox("Really Quit?", "Are you sure you want to quit?") == DialogResult.Yes)
{
if (YesNoMessageBox("Save Before Quit?", "Would you like to save your settings before you Quit?") == DialogResult.Yes)
{
_debug("Settings Saved");
//Properties.App.Default.Save();
//Properties.Settings.Default.Save();
}
}
else
{
a.Cancel = true;
}
}
}
}
這用於檢查用戶是否要退出,並保存用戶是否要退出。 但是,我有兩個選項可以設置為在調用此方法時忽略詢問保存或詢問退出。 比較我想到的這些方法的唯一方法是上面的方法,我相信您可以做到,因此重復的次數要少得多。 (由於該應用仍在開發中,並且尚未完成設置文件,因此保存方法已被注釋掉)。
有人可以幫忙嗎?
我不知道您的參數b
做什么,請對您的變量命名進行操作。 看來這是真的,應該跳過所有問題並退出。 然后可以將其命名為forceQuit
。
應該這樣做:
public void QuitApplication(FormClosingEventArgs closeArgs, bool forceQuit)
{
if (forceQuit)
{
return;
}
if (Properties.App.Default.AskToExit)
{
if (YesNoMessageBox("Really Quit?", "Are you sure you want to quit?") != DialogResult.Yes)
{
// Cancel exit
closeArgs.Cancel = true;
return;
}
}
if (Properties.App.Default.AskToSave)
{
if (YesNoMessageBox("Save Before Quit?", "Would you like to save your settings before you Quit?") == DialogResult.Yes)
{
// Save
}
}
}
我想說第二個消息框應該是Yes|No|Cancel
,其中Cancel取消退出,或者甚至更好的是,根本不使用消息框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.