2017-06-19 44 views
-2
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块之后的代码也会执行。我如何阻止它发生?如何停止c#中的程序流?

+0

*“当我离开一些领域,如果我提交表单,我收到一条消息。”* - 请准确描述你的意思:什么字段?哪里?什么信息? –

+0

如果你想退出一个循环,你可以使用“break”关键字。 – ckuri

+0

如果我使用break,它会从foreach循环中出来,它也会在foreach块后执行代码。请参阅我已附上的代码 – SATYA

回答

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 
+0

感谢您的意见。我会尝试使用它 – SATYA

+0

这个代码中有一个小问题。如果我离开所有的字段,控件不会首先进入if if(cnt是TextBox) if(cnt.Text == string.Empty) { MessageBox.Show(“All fields are mandatory”); } } – SATYA

0

您可以创建自定义异常来控制应用程序的流程。创建一个从异常继承的类

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); 
    } 
}