You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WPF表单中通过C#仅针对特定下拉选项执行验证?

解决TopSeal/BottomSeal仅特定条件下的非空验证问题

当前代码存在逻辑错误:无论选中的Compartment和Purpose是什么,都会强制验证TopSeal和BottomSeal的非空性。但需求是仅当Compartment为"truck-empty"且Purpose为"Leaving-Finished Product"时,才需要对这两个字段做非空验证。

问题根源

现有代码中,判断特定条件的if语句块是空的,后续的TopSeal/BottomSeal验证代码没有被包裹在这个条件块内,导致这些验证逻辑会无条件执行,完全忽略了前置的条件判断。

修改后的完整代码

private void btnSaveTicket_Click(object sender, RoutedEventArgs e)
{
    try
    {
        if (txtFullWeight.Text == "" || txtFullWeight.Text == "0")
        {
            MessageBox.Show("Capture weight before saving");
            Managers.WeighbridgeLogManager.InsertWBInfoLog("Capture weight before saving", "");
            return;
        }
        if (cbxCompartment.SelectedIndex == 0)
        {
            MessageBox.Show("Please select a Compartment", "No compartment selected");
            Managers.WeighbridgeLogManager.InsertWBInfoLog("Please select a Compartment", "");
            return;
        }
        if (cbxCustomer.SelectedIndex == 0)
        {
            MessageBox.Show("Please select a Customer", "No customer selected");
            Managers.WeighbridgeLogManager.InsertWBInfoLog("Please select a customer", "");
            return;
        }
        if (cbxProduct.SelectedIndex == 0)
        {
            MessageBox.Show("Please select a Product", "No product selected");
            Managers.WeighbridgeLogManager.InsertWBInfoLog("Please select a product", "");
            return;
        }

        if (txtBinsSelected.Text == "")
        {
            MessageBox.Show("Please select a Bin", "No Bin selected");
            Managers.WeighbridgeLogManager.InsertWBInfoLog("Please select a Bin", "");
            return;
        }
        if (comboPurpose.SelectedIndex == 0)
        {
            MessageBox.Show("Please select a Purpose", "No purpose selected");
            Managers.WeighbridgeLogManager.InsertWBInfoLog("Please select a purpose", "");
            return;
        }

        if (txtFullWeight.Text == "")
        {
            MessageBox.Show("Please capture weight", "No Weight entered");
            Managers.WeighbridgeLogManager.InsertWBInfoLog("Please capture weight", "");
            return;
        }

        // 仅当满足特定条件时,才验证TopSeal和BottomSeal
        if (cbxCompartment.SelectedItem != null &&
            string.Equals(cbxCompartment.SelectedItem.ToString(), "truck-empty", StringComparison.OrdinalIgnoreCase) &&
            cbxPurpose.SelectedItem != null &&
            string.Equals(cbxPurpose.SelectedItem.ToString(), "Leaving-Finished Product", StringComparison.OrdinalIgnoreCase))
        { 
            // 检查TopSeal非空
            if (string.IsNullOrEmpty(txtTopSeal.Text.Trim()))
            {
                MessageBox.Show("TopSeal cannot be empty when the truck is empty and the purpose is Leaving-Finished Product", "Validation Error");
                txtTopSeal.Focus();
                return;
            }

            // 检查BottomSeal非空
            if (string.IsNullOrEmpty(txtBottomSeal.Text.Trim()))
            {
                MessageBox.Show("BottomSeal cannot be empty when the truck is empty and the purpose is Leaving-Finished Product", "Validation Error");
                txtBottomSeal.Focus();
                return;
            }
        }

        presenter.SaveTicket();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error");
        Managers.WeighbridgeLogManager.InsertWBErrorLog(ex.Message, TripSheetNo.ToString() == "" ? "" : TripSheetNo.ToString(), ex.StackTrace, ex.InnerException == null ? "" : ex.InnerException.ToString());
        return;
    }
}

修改说明

  • 将TopSeal和BottomSeal的非空验证逻辑,完整嵌套到判断Compartment == "truck-empty" && Purpose == "Leaving-Finished Product"的if代码块内部
  • 这样只有当选中的Compartment和Purpose同时满足指定值时,才会触发这两个字段的非空检查;其他情况会直接跳过该验证逻辑,执行后续的保存操作

内容的提问来源于stack exchange,提问作者thrue2023

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 11:25:05