いくつかの要素を含むカスタム ユーザー コントロールがあるので、次のようになります。
UserControl
Textbox
Button
....
タイトルで述べたように、ボタンをクリックすると、テキストボックスはフォーカスを失い、その Validating イベントが発生します...しかし、ボタンのクリックは決して発生しません...これは通常の動作だと思います...しかし、何検証が失敗しなかった場合にボタンの Click イベントを実行する適切な方法はありますか?
検証に使用するコードは次のとおりです。
private void txtPassword_Validating(object sender, CancelEventArgs e)
{
string errorMessage = _uiValidator.ValidatePassword(txtPassword.Text);
errRegistration.SetError(txtPassword, errorMessage);
}
したがって、errRegistration.SetError() は正常に実行されました - errorMessage は null であり、検証エラーがないことを意味するので、問題ないと思います (ブレークポイントを配置してこれを確認しました)。
そして、これはボタンの Click イベントの実装です:
private void btnRegisterUser_Click(object sender, EventArgs e)
{
_uiValidator.ValidatePassword(txtUsername.Text);
//validate other fields here...
if (_uiValidator.IsValid())
{
this._controller.Save();
}else{
MessageBox.Show("...", "Title", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}