【问题标题】:How can I show message boxes depending on a bool result?如何根据布尔结果显示消息框?
【发布时间】:2012-12-04 10:39:20
【问题描述】:

我的 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;
            }
        }
    }
}

这用于检查用户是否想要退出并在他们想要时保存。但是,我有两个选项可以设置为在调用此方法时忽略要求保存或要求退出。我想到的比较这些的唯一方法是上面的,我相信你可以做到,所以重复次数要少得多。 (保存方式已被注释掉,因为应用仍在开发中,设置文件尚未完成)。

谁能帮忙?

【问题讨论】:

  • 在退出应用程序之前问这么多事情真的很烦人
  • if(!c && !d) 应该怎么做?
  • abcd 真的有用吗?如果我是一名新开发人员,正在查看您的代码,这到底是什么意思?
  • 以及@Habib 所说的;这会非常烦人(也是我曾经犯过的一个错误)
  • 它们并不是真正有用的名称,不,但没有其他人会接受此代码,我有点强迫症。 C 和 D 是特定设置选项的值。 @habib 是的,我知道,这就是为什么我添加了删除它们的选项。

标签: c# boolean


【解决方案1】:

我不知道你的参数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 取消退出,或者更好的是,根本不使用消息框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 2016-12-19
    • 1970-01-01
    • 2013-12-25
    相关资源
    最近更新 更多