如何使多个返回true的条件在C#中执行?

问题描述

我有一个checkedListBox,用户可以在其中选择想要更新的内容。我希望他们能够自由更新1台机器的5个特征。因此,当他们只想更新1件东西时,就不必提供其他4个特性。另外,当他们想要更新5个特征时, 一口气就能做到。为此,我有以下if语句:


if (clbCharacteristicsToUpdate.CheckedItems.Count != 0)
{
                        if (clbCharacteristicsToUpdate.GetSelected(0))
                        {
                            currentITime = Convert.ToDouble(tbCurrentITime.Text);
                            MessageBox.Show(currentITime.ToString());
                            dh.UpdateCurrentITime(machineNr,currentITime);
                        }
                        if (clbCharacteristicsToUpdate.GetSelected(1))
                        {
                            cycleTime = Convert.ToDouble(tbCycleTime.Text);
                            MessageBox.Show(cycleTime.ToString());
                            dh.UpdateCycleTime(machineNr,cycleTime);
                        }
                        if (clbCharacteristicsToUpdate.GetSelected(2))
                        {
                            nrOfLinesPerCm = Convert.ToInt32(tbNrOfLinesPerCm.Text);
                            MessageBox.Show(nrOfLinesPerCm.ToString());
                            dh.UpdateNrOfLinesPerCm(machineNr,nrOfLinesPerCm);
                        }
                        if (clbCharacteristicsToUpdate.GetSelected(3))
                        {
                            heightOfLamallae = Convert.ToDouble(tbHeightOfLamallae.Text);
                            MessageBox.Show(heightOfLamallae.ToString());
                            dh.UpdateHeightOfLamallae(machineNr,heightOfLamallae);
                        }
                        if (clbCharacteristicsToUpdate.GetSelected(4))
                        {
                            if (rbLTB.Checked)
                            {
                                machineType = 2;
                                MessageBox.Show(machineType.ToString());
                            }
                            else if (rbSTB.Checked)
                            {
                                machineType = 1;
                                MessageBox.Show(machineType.ToString());
                            }
                            if(!rbLTB.Checked && !rbSTB.Checked)
                            {
                                MessageBox.Show("Select a machine type to update!");
                                return;
                            }
                            dh.UpdateType(machineNr,machineType);  
                         }

}

我的问题是,当我选择并更新1件东西时,它可以很好地工作。但是当我选择多个时,它只会执行最后一个返回true的if语句。我考虑过使用if-else,但是只有第一个返回true的将被执行。我还考虑过为每种可能性使用if语句。但是,由于我可以更新5个特征,因此有25种可能性,而我不想有25条if语句。预先感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)