简体   繁体   English

简化if-else表达式

[英]Simplify if-else expression

I am trying to manage a 3-result dialog, in if ... else , but I find it hard to use. 我尝试在if ... else管理一个3结果对话框,但是我发现它很难使用。 This is my code: 这是我的代码:

if (MessageBox.Show("Are you sure you want to exit", "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes) {
   MessageBox.Show("Why?!", "", MessageBoxButtons.RetryCancel);
   Application.Restart();
} else if (MessageBox.Show("Are you sure you want to exit", "", MessageBoxButtons.YesNoCancel) == DialogResult.No) {
    if (MessageBox.Show("OK ^_^! Good Luck!", "", MessageBoxButtons.OK) == DialogResult.OK) 
        Application.Restart();
} else {
    if (MessageBox.Show("Are you sure you want to cancel?", "", MessageBoxButtons.OKCancel) == DialogResult.OK) {
        MessageBox.Show("Ok! Good Luck!");
        Application.Restart();
    } else {
        MessageBox.Show("Error!");
        Application.Restart();
    }
}

Whenever I run it, if I press "No" or "Cancel", it opens a new dialog. 每当我运行它时,如果我按“否”或“取消”,它将打开一个新对话框。 How can I avoid that? 我该如何避免呢?

You should first get the DialogResult and then use it in the if-else statement: 您应该首先获取DialogResult ,然后在if-else语句中使用它:

DialogResult result = MessageBox.Show("Are you sure you want to exit", "", MessageBoxButtons.YesNoCancel);

if(result == DialogResult.Yes)
{
    //Code if Ok
}
else if(result == DialogResult.No)
{
    //Code if No
}
else
{
    //Code if Cancel
}

This way the MessageBox will open only once 这样, MessageBox将仅打开一次

Your first if statement is checking for a result of Yes : 您的第一个if语句正在检查Yes的结果:

if (MessageBox.Show("Are you sure you want to exit", "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes) 

If the user selects No or Cancel , you move on to the next if statement which produces another MessageBox : 如果用户选择“ No或“ Cancel ,则转到下一个生成另一个MessageBox if语句:

else if (MessageBox.Show("Are you sure you want to exit", "", MessageBoxButtons.YesNoCancel) == DialogResult.No) {

Finally, if the user selects 'Cancel', you get yet another MessageBox : 最后,如果用户选择“取消”,则会得到另一个MessageBox

else {
    if (MessageBox.Show("Are you sure you want to cancel?", "", MessageBoxButtons.OKCancel) == DialogResult.OK) 
     //Other code here...
     }

This is why you see multiple MessageBox instances. 这就是为什么您看到多个MessageBox实例的原因。

So, you should only show one box, then work with the result. 因此,您应该只显示一个框,然后使用结果。 However, the code suggests that additional information must be gathered on certain instances: 但是,该代码建议必须在某些实例上收集其他信息:

var result = MessageBox.Show("Are you sure you want to exit", "", MessageBoxButtons.YesNoCancel);
switch(result)
{
    case DialogResult.Yes:
        //Another box pops up to ask the user why
        MessageBox.Show("Why?!", "", MessageBoxButtons.RetryCancel);
        Application.Restart();
        break;
    case DialogResult.No:
        //Informational box
        MessageBox.Show("OK ^_^! Good Luck!", "", MessageBoxButtons.OK);
        Application.Restart();
    default:
        //Assume Cancel to be the default behavior, 
        //Pick any value to be the default. It's up to you.
        //Make sure they really, REALLY want to cancel
        if (MessageBox.Show("Are you sure you want to cancel?", "", MessageBoxButtons.OKCancel) == DialogResult.OK) 
        {
            MessageBox.Show("Ok! Good Luck!");
            Application.Restart();
        } 
        else 
        {
            MessageBox.Show("Error!");
            Application.Restart();
        }
}

I don't think your code should be calling Application.Restart at the false branch of the Cancel alternative, but the logic is up to you. 我认为您的代码不应该调用Application.Restart 。在Cancel替代方法的false分支处重新启动,但是逻辑取决于您。

As others have pointed out, please try to properly indent/format your code. 正如其他人指出的那样,请尝试正确缩进/格式化您的代码。 It will make it easier for you and others to understand. 这将使您和其他人更容易理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM