如何使用消息框的switch语句重写此代码,以使代码更小更快?
// panel1.Visible = checkBoxCCF.Checked;
panel2.Visible = checkBoxReliabilty.Checked;
panel3.Visible = checkBoxRisk.Checked;
panel4.Visible = checkBoxSaftey.Checked;
panel5.Visible = checkBoxSensitivity.Checked;
panel6.Visible = checkBoxThroughput.Checked;
panel7.Visible = checkBoxUncertainity.Checked;
if (checkBoxCCF.Checked)
{
DialogResult result = MessageBox.Show("Do you do want to know more about " + checkBoxCCF.Text, "Warning",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
//code for yes
}
else if (result == DialogResult.No)
{
panel1.Visible = checkBoxCCF.Checked;
}
else if (result == DialogResult.Cancel)
{
//code for Cancel
}
答案 0 :(得分:2)
您可以在枚举上switch
:
switch (result)
{
case DialogResult.Yes:
// Code for yes...
break;
case DialogResult.No:
// Code for no...
break;
case DialogResult.Cancel:
// Code for cancel...
break;
}
然而,这不太可能更快。我赞成在给定的例子中看起来最干净的方法。