如何使用c#从wpf表单中仅选择下拉列表中的特定字段?

问题描述 投票:0回答:1

我有一个条件,检查隔间(卡车空)和用途(“离开成品”),这些是我唯一必填的字段,选择时必须与顶部密封和底部密封一起进行验证并忽略其他隔间进行验证,现在现在的问题是,所有隔间似乎都在处理这种逻辑,并希望防止它发生。我怎样才能实现这个目标?

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 (comboPurpose.Text.ToUpper() != "SELECT A PURPOSE")
        //{
            if (txtFullWeight.Text == "")
            {
                MessageBox.Show("Please capture weight", "No Weight entered");
                Managers.WeighbridgeLogManager.InsertWBInfoLog("Please capture weight", "");
                return;
            }


        // Check if the compartment selected is "truck-empty" and the purpose is "Leaving-Finished Product"
        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))
        { 
        }
            // Check if both TopSeal and BottomSeal are empty
            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;
            }

            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;
    }
}
c# wpf data-binding
1个回答
0
投票

您可能丢失了支架:

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))
{ // This statement block is empty!
} // <=== Move this brace down!

将右大括号

}
移至其他两个条件之后:

// Check if the compartment selected is "truck-empty" and the purpose is "Leaving-Finished Product"
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))
{
    // Check if both TopSeal and BottomSeal are empty
    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;
    }

    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;
    }
} // <=== The brache is here now!
presenter.SaveTicket();
© www.soinside.com 2019 - 2024. All rights reserved.