为什么我的方法相互干扰并干扰 C# 中的 if 语句?

Why my methods interfering with each others and with if statements in C#?

我有一个表格可以根据某些条件在 sql 数据库中插入一些数据,首先我需要检查空值并提醒用户:

void CheckNulls() {

    try {
        if (txtcode.Text.Length == 0 || txtItem.Text.Length == 0 || txtWh.Text.Length == 0) {

            MessageBox.Show("Fill Required Fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        else {
            checkExistAndDo();

        }

    }

    catch(Exception Err) {
        MessageBox.Show("This Error Occured :" + Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

}

然后我需要检查用户是否勾选了 checkbox :

void checkExistAndDo() {

    if (chkProduct.Checked || chkMaterial.Checked)

    {

        if (chkProduct.Checked == true) {
            if (!chkMaterial.Checked) {
                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) {

                    MessageBox.Show("Existing Code", "Error");

                }
                else {

                    TakeAction();
                }
            }
        }

        else {
            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("Existing Code", "Error");

            }
            else {

                TakeAction();
            }

        }

    }

    else {
        MessageBox.Show("Check even one", "check", MessageBoxButtons.OK, MessageBoxIcon.Warning);

    }

}

所以,我总共有 4 种不同的组合:

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

然后我需要根据组合采取行动:

private void TakeAction()
{
    try
    {

        if (chkProduct.Checked == true && chkMaterial.Checked == false)
        {


            InsertProduct();
            MessageBox.Show("Product Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        else if (chkProduct.Checked == false && chkMaterial.Checked == true)
        {

            InsertMaterial();
            MessageBox.Show("Material Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        else
        {
            InsertSubProduct();
            MessageBox.Show("SubProduct Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }

    catch (Exception Err)
    {
        MessageBox.Show("This Error Occured :" + Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    finally
    {
        this.Close();
    }


}

问题是这部分没有做任何不显示我的消息的事情,甚至没有错误消息,就好像它不存在于我的代码中一样:

       else
        {
            InsertSubProduct();
            MessageBox.Show("SubProduct Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

很抱歉 post 但我已尽力并多次调试代码,但我无法通过此操作,在此先感谢您的帮助。

如果 ThreeState 属性 设置为真,选中 属性 将 return true CheckedIndeterminate检查状态。

选中 ThreeState 属性 的 chkMaterial 复选框

根据@Joel Coehoorn 的评论,我尝试了不同的方法 我引用“更好地分离关注点。CheckNulls() 应该只 return 一个布尔值,并且不向用户显示任何消息或调用任何其他方法。checkExistAndDo() 根本不应该存在(该逻辑应该完全在数据库中作为 InsertProduct/MaterialSubproduct() 的一部分)。“

效果很好,感谢大家的回复,感谢您的指导。