2017-06-19 91 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); 
    } 
}