2010-01-03 2 views
6

winform을 취소하기 위해 취소 버튼을 클릭 할 때 errorprovider의 유효성을 우아하게 비활성화하는 방법이 있습니까? 텍스트 상자가 포커스를 잃었을 때 항상 유효성 검사가 수행되며 사용자가 취소 버튼을 클릭 할 때 유효성을 검사하지 않으려는 경우 사용자가 취소를 클릭했을 때 유효성을 검사하는 것이 약간 바보입니다.취소 버튼을 클릭하면 errorprovider의 유효성 검사가 비활성화됩니다.

답변

14

Google 검색 결과를 찾은 후 취소 버튼의 CauseValidation 속성을 false로 설정하면됩니다. 그게 다야.

5

방금이 문제가 발생하여 CauseValidation = false 설정은 부분적인 해결책 일뿐입니다.

취소 단추로 Form.CancelButton을 설정하면 Esc 키가 해당 단추를 호출합니다. 그러나 CauseValidation = false으로 설정하더라도 Escape 키에 대한 응답으로 유효성 검사가 계속 실행됩니다.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
{ 
    // Although we set CausesValidation = false for the Cancel button, 
    // the Escape key fails to cancel due to validation failure. The 
    // Form.CancelButton property should invoke the validation-free 
    // cancel button, but doesn't. Force the issue here. 
    if (keyData == Keys.Escape) 
    { 
     DialogResult = DialogResult.Cancel; 
     return true; 
    } 
    return base.ProcessCmdKey(ref msg, keyData); 
} 
:이 문제를 해결하려면

, 다음과 같은 해킹을 추가