弹出C#两个消息框 - 需要停止一个

时间:2014-11-26 18:15:19

标签: c#

当没有按钮被选中时,两个消息框都会相互运行,只显示以下内容:

{ MessageBox.Show("Incomplete order. Please review.", "Incomplete Order."); }

如果未选中任何按钮,如何防止以下消息框运行?

MessageBox.Show("You have ordered a " + pizzaChoice, "Order Confirmation.");

"代码"

private void ConfirmOrder_Click(object sender, EventArgs e)
{
    string pizzaChoice = "";
    if (NeapolitanStyle.Checked == false & NEGreekStyle.Checked == false & ChicagoStyle.Checked == false & SmallPizza.Checked == false & MediumPizza.Checked == false & LargePizza.Checked == false)
    { 
        MessageBox.Show("Incomplete order. Please review.", "Incomplete Order."); 
    }
    if (SmallPizza.Checked)
    {
        pizzaChoice = pizzaChoice + SmallPizza.Text + " "; 
    }
    if (MediumPizza.Checked)
    {
        pizzaChoice = pizzaChoice + MediumPizza.Text + " "; 
    }
    if (LargePizza.Checked)
    {
        pizzaChoice = pizzaChoice + LargePizza.Text + " "; 
    }
    if (NEGreekStyle.Checked)
    {
        pizzaChoice = pizzaChoice + NEGreekStyle.Text + " pizza" + "\n";
    }
    if (ChicagoStyle.Checked)
    {
        pizzaChoice = pizzaChoice + ChicagoStyle.Text + " pizza" + "\n";
    }
    if (NeapolitanStyle.Checked)
    {
        pizzaChoice = pizzaChoice + NeapolitanStyle.Text + " pizza" + "\n"; 
    }
    if (VeryHotChilis.Checked)
    {
        pizzaChoice = pizzaChoice + "& " + VeryHotChilis.Text + "." + "\n"; 
    }
    if (Onions.Checked)
    {
        pizzaChoice = pizzaChoice + "& " + Onions.Text + "." + "\n"; 
    }
    if (Mushrooms.Checked)
    {
        pizzaChoice = pizzaChoice + "& " + Mushrooms.Text + "."  + "\n"; 
    }
    MessageBox.Show("You have ordered a " + pizzaChoice, "Order Confirmation.");
}

5 个答案:

答案 0 :(得分:1)

如果你改变了

,你会走很长的路
 if (SmallPizza.Checked)

 else if (SmallPizza.Checked)

答案 1 :(得分:0)

出现第一条错误消息后,您可以退出该功能。只需在邮箱

之后放置return;即可

答案 2 :(得分:0)

如果Frank Pytel提出建议,那么对其他人的轻微变化。而不是将后续的if更改为else,如果它需要是第一次验证检查的if else,则else块中的if语句系列如下所示。

private void ConfirmOrder_Click(object sender, EventArgs e)
{
    string pizzaChoice = "";
    if (NeapolitanStyle.Checked == false & NEGreekStyle.Checked == false & ChicagoStyle.Checked == false & SmallPizza.Checked == false & MediumPizza.Checked == false & LargePizza.Checked == false)
    { 
        MessageBox.Show("Incomplete order. Please review.", "Incomplete Order."); 
    }
    else {
        if (SmallPizza.Checked)
        {
            pizzaChoice = pizzaChoice + SmallPizza.Text + " "; 
        }
        if (MediumPizza.Checked)
        {
            pizzaChoice = pizzaChoice + MediumPizza.Text + " "; 
        }
        if (LargePizza.Checked)
        {
            pizzaChoice = pizzaChoice + LargePizza.Text + " "; 
        }
        if (NEGreekStyle.Checked)
        {
            pizzaChoice = pizzaChoice + NEGreekStyle.Text + " pizza" + "\n";
        }
        if (ChicagoStyle.Checked)
        {
            pizzaChoice = pizzaChoice + ChicagoStyle.Text + " pizza" + "\n";
        }
        if (NeapolitanStyle.Checked)
        {
            pizzaChoice = pizzaChoice + NeapolitanStyle.Text + " pizza" + "\n"; 
        }
        if (VeryHotChilis.Checked)
        {
            pizzaChoice = pizzaChoice + "& " + VeryHotChilis.Text + "." + "\n"; 
        }
        if (Onions.Checked)
        {
            pizzaChoice = pizzaChoice + "& " + Onions.Text + "." + "\n"; 
        }
        if (Mushrooms.Checked)
        {
            pizzaChoice = pizzaChoice + "& " + Mushrooms.Text + "."  + "\n"; 
        }
        MessageBox.Show("You have ordered a " + pizzaChoice, "Order Confirmation.");
    }
}

答案 3 :(得分:0)

您也可以这样做

if (MessageBox.Show("First MessageBox", "Test", MessageBoxButtons.OK) == System.Windows.Forms.DialogResult.OK) {

          MessageBox.Show("Second messageBox", "Test", MessageBoxButtons.OK);

}

答案 4 :(得分:0)

好的进入回归;做了伎俩。我尝试将'else if'放入,但两个消息框仍然弹出。

谢谢你们。最感谢。