如何在c#中停止程序的流程?

时间:2017-06-19 17:36:55

标签: c#

foreach (Control cnt in panel1.Controls)
{
    if (cnt is TextBox)
    {
        if (cnt.Text == string.Empty)
        {
            MessageBox.Show("All fields are mandatory");

        }
    }
    else if (cnt is ComboBox)
    {
        ComboBox cmb = (ComboBox)cnt;
        if (cmb.SelectedIndex == -1)
        {
            MessageBox.Show("All fields are mandatory");
            Application.Exit();

        }
    }
}

string gender;
string dob = cmbDate.Text + "/" + cmbMonth.Text + "/" + cmbYear.Text;
if (rbMale.Checked == true)
    gender = rbMale.Text;
else
    gender = rbFemale.Text;

query = "Insert into Admissions(Admission_date,Student_name,Father_name,Mother_name,DOB,Gender,Address,State, City,Pincode,Admission_for,Previous_school,Fees)values('" + txtAdmDate.Text + "','" + txtStudentName.Text + "','" + txtFatherName.Text + "','" + txtMotherName.Text + "','" + dob + "','" + gender + "','" + txtAddress.Text + "','" + txtState.Text + "','" + txtCity.Text + "','" + txtPincode.Text + "','" + cmbClass.Text + "','" + txtPreviousSchool.Text + "','" + txtFees.Text + "'); SELECT SCOPE_IDENTITY();";

cmd = new SqlCommand(query, con);
con.Open();
int admId = (int)cmd.ExecuteScalar();
con.Close();

当我遗漏某些字段时,如果我提交表单,我会收到一条消息。但是当我单击OK时,foreach块之后的代码也会执行。我该如何阻止它发生?

2 个答案:

答案 0 :(得分:0)

有时,无论你看到什么,Application.Exit()都有不良的副作用。尝试使用标志来保证在停止条件之后不会无意中运行代码。

var isExiting = false; 
foreach (Control cnt in  panel1.Controls)
        {
            if (cnt is TextBox)
            {
                if(cnt.Text==string.Empty)
                {
                    MessageBox.Show("All fields are mandatory");

                }
            }
            else if(cnt is ComboBox)
            {
                ComboBox cmb = (ComboBox)cnt;
                if(cmb.SelectedIndex == -1)                
                {
                    isExiting = true;
                    MessageBox.Show("All fields are mandatory");
                    Application.Exit();

                }                      
            }
        }

    if(!isExiting){
        string  gender;
        string dob = cmbDate.Text + "/" +cmbMonth.Text + "/"+cmbYear.Text;
        if (rbMale.Checked == true)
            gender = rbMale.Text;
        else
            gender = rbFemale.Text;

        query = "Insert into Admissions(Admission_date,Student_name,Father_name,Mother_name,DOB,Gender,Address,State, City,Pincode,Admission_for,Previous_school,Fees)values('" + txtAdmDate.Text + "','" + txtStudentName.Text + "','" + txtFatherName.Text + "','" + txtMotherName.Text + "','" + dob + "','" + gender + "','" + txtAddress.Text + "','" + txtState.Text + "','" + txtCity.Text + "','" + txtPincode.Text + "','" + cmbClass.Text + "','" + txtPreviousSchool.Text + "','" + txtFees.Text + "'); SELECT SCOPE_IDENTITY();";

        cmd = new SqlCommand(query,con);
        con.Open();
        int admId = (int)cmd.ExecuteScalar();
        con.Close();
    }//if !isExiting

答案 1 :(得分:0)

您可以创建自定义例外来控制应用程序的流程。创建一个继承自Exception

的类
public class FieldAreMandatoryException : Exception {
}

并在发生错误时抛出它。这将停止应用程序的流程并触发catch块。

try
{
    foreach (Control cnt in panel1.Controls)
    {
        if (cnt is TextBox)
        {
            if (cnt.Text == string.Empty)
            {
                throw new FieldAreMandatoryException(); //this will stop the flow and continue in the catch clause
            }
        }
        else if (cnt is ComboBox)
        {
            ComboBox cmb = (ComboBox) cnt;
            if (cmb.SelectedIndex == -1)
            {
                throw new FieldAreMandatoryException(); //this will stop the flow and continue in the catch clause
            }
        }
    }
    string gender;
    string dob = cmbDate.Text + "/" + cmbMonth.Text + "/" + cmbYear.Text;
    if (rbMale.Checked == true)
        gender = rbMale.Text;
    else
        gender = rbFemale.Text;
    query =
        "Insert into Admissions(Admission_date,Student_name,Father_name,Mother_name,DOB,Gender,Address,State, City,Pincode,Admission_for,Previous_school,Fees)values('" +
        txtAdmDate.Text + "','" + txtStudentName.Text + "','" + txtFatherName.Text + "','" +
        txtMotherName.Text + "','" + dob + "','" + gender + "','" + txtAddress.Text + "','" +
        txtState.Text +
        "','" + txtCity.Text + "','" + txtPincode.Text + "','" + cmbClass.Text + "','" +
        txtPreviousSchool.Text + "','" + txtFees.Text + "'); SELECT SCOPE_IDENTITY();";

    cmd = new SqlCommand(query, con);
    con.Open();
    int admId = (int) cmd.ExecuteScalar();
    con.Close();
}
catch (FieldAreMandatoryException exception) {
        Console.Log("All fields are mandatory!");
        Environment.Exit(0);
    }
}