2017-06-19 8 views
-2

フィールドを省略してフォームを送信すると、メッセージが表示されます。しかし、[OK]をクリックすると、foreachブロックの後のコードも実行されます。どのように起こるのをやめさせるのですか?私はどのようにプログラムの流れをC#で止めますか?

+0

*「フィールドを省略してフォームを送信するとメッセージが表示されます」* - 正確な内容を記述してください:どのフィールド?どこ?どのようなメッセージですか? –

+0

ループを終了する場合は、「break」キーワードを使用できます。 – ckuri

+0

私はブレークを使用する場合、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

このコードでは小さな問題があります。 (cntがTextBoxの場合)ブロックの場合、コントロールが最初に入ることはありません。 { if(cnt.Text == string.Empty) { MessageBox.Show( "すべてのフィールドは必須です"); } } – SATYA

0

カスタム例外を作成して、アプリケーションのフローを制御することができます。例外を継承するクラスを作成してください。

public class FieldAreMandatoryException : Exception { 
} 

エラーが発生した場合は、投げてください。これにより、アプリケーションのフローが停止し、キャッチブロックがトリガーされます。

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); 
    } 
} 
関連する問題