有条件的对话框

本文关键字:对话框 有条件 | 更新日期: 2023-09-27 18:07:36

如果满足不同的条件,我目前正在制作一个脚本来显示特定的MessageBoxes。我有8个条件要检查,如果它们是"0"或"1",每个条件都必须显示不同的MessageBoxes

我的代码的一个简短的例子如下:
// Similar if(y[1] == "1") statements above with similar Messages but without the corresponding fruit(s)
if (y[2] == "1")
{ MessageBox.Show("Multiple goods required! Please get the following
                   items off the shelves" + Environment.NewLine +
                   "1. Apple" + Environment.NewLine + "2. Pear" +
                   Environment.NewLine + "3. Orange");
}
else if (y[2] == "0")
{ MessageBox.Show("Multiple goods required! Please get the following
                   items off the shelves" + Environment.NewLine +
                   "1. Apple" + Environment.NewLine + "2. Pear");
}

我的c#知识很基础,但我愿意学习!请帮助!

有条件的对话框

我将有条件地生成消息,并编写显示一次消息框的代码:

string msg = "Multiple goods required! Please get the following items off the shelves";
if(y[2] == "0" || y[2] == "1")
{
    msg += Environment.NewLine + "1. Apple"
            + Environment.NewLine + "2. Pear";
    if (y[2] == "1")
        msg += Environment.NewLine + "3. Orange";
    MessageBox.Show(msg);
}

我只是让上面的代码与你的代码相等,否则我认为它可以写得更好。

也可以考虑使用String。格式和StringBuilder来创建消息,而不是连接小字符串。

如果你的问题是如何更容易地显示消息框,它将是这样的:

for(int i =0;i<y.length;i++){
            if(y[i] == 0){
                MessageBox.Show("Multiple goods required! Please get the following
                   items off the shelves" + Environment.NewLine +
                   "1. Apple" + Environment.NewLine + "2. Pear");
            }
            else if(y[i] == 1){
                MessageBox.Show("Multiple goods required! Please get the following
                   items off the shelves" + Environment.NewLine +
                   "1. Apple" + Environment.NewLine + "2. Pear" +
                   Environment.NewLine + "3. Orange");
            }
        }

如果这不是你的问题,请告诉我我会回来帮忙的