为什么最后一个条件 else 不能正常工作 C#

Why this last condition else doesn't work properly C#

我有这个 (if-else if-else) 语句它工作正常除了 else 部分我不知道为什么?

void TakeAction()
{
    try
    {
        if (chkProduct.Checked == true && chkMaterial.Checked == false)
        {
            InsertProduct();
            MessageBox.Show("Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }

        else if (chkProduct.Checked == false && chkMaterial.Checked == true)
        {
            InsertMaterial();
            MessageBox.Show("Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);

            this.Close();
        }

        else if (chkProduct.Checked == true && chkMaterial.Checked == true)
        {
            InsertSubProduct();
            MessageBox.Show("Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);

            this.Close();
        }

        // else if (chkProduct.CheckState == CheckState.Unchecked && chkMaterial.CheckState == CheckState.Unchecked) // Tried this also still nothing
        else
        //Doesn't work
        {
            MessageBox.Show("Check even one Checkbox", "Choose", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
    catch (Exception Err)
    {
        MessageBox.Show("This Error Occured :" + Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

}

我尝试了不同的方式,就像 else , else if 最后代码中的注释行。 提前致谢。

编辑#1 我认为此方法干扰了 TakeAction 方法并导致了问题,我将从一开始就调试我的代码,谢谢

        if (chkProduct.Checked == true && chkMaterial.Checked == false)
        {
            da = new SqlDataAdapter("SELECT [ProductCode] FROM Products Where [ProductCode] = @prcode ", Cn);
            da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
            dt.Clear();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                //int cnt = Convert.ToInt32(dt.Rows[0]["ProductCode"].ToString());
                MessageBox.Show("Duplicated", "Duplicated");

            }
            else
            {
                //MessageBox.Show("No problem , Its not Product");
                TakeAction();
            }
        }

        //the first If >> If Material Or SubProduct
        else if (chkMaterial.Checked == true)
        {
            da = new SqlDataAdapter("SELECT Code FROM Items Where Code = @prcode ", Cn);
            da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
            dt.Clear();

            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("Duplicated", "Duplicated");

            }
            else
            {
                // MessageBox.Show("No problem , Its not item");
                TakeAction();
            }

如果 else 语句不起作用,则意味着 else if 语句之一被命中。你试过断点吗?您还可以删除 true 和 false 值以使其更具可读性。

而不是:

if (x == true && y == false) { }

您可以执行以下操作:

if (x && !y) { }

您总共有4种不同的组合:

chkProduct.Checked : chkMaterial.Checked : Action
--------------------------------------------------------------
              true :                true : InsertSubProduct()
              true :               false : InsertMaterial()
             false :                true : InsertProduct()
             false :               false : Ask User to Check         

这就是为什么您不必将最后一个条件作为 else if: else 就足够了。如果调试遇到问题,可以将代码更改为 nesting ifs 并将最后一个条件向前推:

if (!chkProduct.Checked && !chkMaterial.Checked) 
  MessageBox.Show("Check even one Checkbox", 
                  "Choose", 
                   MessageBoxButtons.OK, 
                   MessageBoxIcon.Warning);
else {
  if (chkProduct.Checked)
    if (chkMaterial.Checked) 
      InsertSubProduct();
    else 
      InsertProduct();
  else 
    InsertMaterial();

  MessageBox.Show("Done", 
                  "Done", 
                   MessageBoxButtons.OK, 
                   MessageBoxIcon.Information);
  Close();
}