2013-07-30 66 views
0

當用戶單擊確定按鈕,我確認這就像輸入:當驗證規則休息,停止執行並注重形式

private void btnOk_Click(object sender, EventArgs e) 
{ 
    myTxtField_Validated(this, e); 
    myTxtField2_Validated(this, e); 

    CustomObject obj = new CustomObject ..... 
} 

裏面的驗證,我用錯誤提供通知用戶,在字段輸入必須符合一定的標準。

如果驗證中斷,我該如何靜靜地中斷此部分的執行CustomObject obj = new CustomObject ....。它按預期的方式工作,只存在用於驗證的代碼。

回答

1

使用私有布爾成員可以輕鬆地實現這一目標,並實現它是這樣的:

private bool _validatedTextFields; 
private void btnOk_Click(object sender, EventArgs e) 
{ 
    myTxtField_Validated(this, e); 
    myTxtField2_Validated(this, e); 

    if (_validatedTextFields) 
    { 
    CustomObject obj = new CustomObject ..... 
    } 
} 

private void myTxtField_Validated(object sender, EventArgs e) 
{ 
    //your validation... 
    _validatedTextFields = true //only set your member to true if it has been validated. 
} 

private void myTxtField2_Validated(object sender, EventArgs e) 
{ 
    //your validation... 
    _validatedTextFields = true //only set your member to true if it has been validated. 
}