この記事は翻訳者によって翻訳されたものです。 記事の文章にポインターを重ねると、原文のテキストが表示されます。 |
訳文
原文
|
Control.CausesValidation プロパティ
そのコントロールが原因で、フォーカスを受け取ると検証が必要なコントロールに対して、検証が実行されるかどうかを示す値を取得または設定します。
アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)
CausesValidation プロパティが false に設定されている場合、Validating イベントおよび Validated イベントは発生しません。
CausesValidation プロパティ値は、通常、[ヘルプ] ボタンなどのコントロールに対して false に設定されます。
派生クラス TextBox を使用して、ユーザーが入力した電子メール アドレスを検証するコード例を次に示します。 電子メール アドレスが標準の書式 ("@" および "." を含む) ではない場合、検証は失敗し、ErrorProvider アイコンが表示され、イベントはキャンセルされます。 フォーム上のボタンのいずれかで、CausesValidation プロパティが false に設定されています。 このボタンをクリックするかフォーカスを設定しても、検証は発生しません。 この例では、TextBox および ErrorProvider コントロールと、Button がフォーム上で作成されていることが必要です。
public Form1() { InitializeComponent(); //Set button2 to be non-validating. this.button2.CausesValidation = false; } private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e) { string errorMsg; if(!ValidEmailAddress(textBox1.Text, out errorMsg)) { // Cancel the event and select the text to be corrected by the user. e.Cancel = true; textBox1.Select(0, textBox1.Text.Length); // Set the ErrorProvider error with the text to display. this.errorProvider1.SetError(textBox1, errorMsg); } } private void textBox1_Validated(object sender, System.EventArgs e) { // If all conditions have been met, clear the ErrorProvider of errors. errorProvider1.SetError(textBox1, ""); } public bool ValidEmailAddress(string emailAddress, out string errorMessage) { // Confirm that the e-mail address string is not empty. if(emailAddress.Length == 0) { errorMessage = "e-mail address is required."; return false; } // Confirm that there is an "@" and a "." in the e-mail address, and in the correct order. if(emailAddress.IndexOf("@") > -1) { if(emailAddress.IndexOf(".", emailAddress.IndexOf("@") ) > emailAddress.IndexOf("@") ) { errorMessage = ""; return true; } } errorMessage = "e-mail address must be valid e-mail address format.\n" + "For example 'someone@example.com' "; return false; }
Windows 7, Windows Vista SP1 以降, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core はサポート対象外), Windows Server 2008 R2 (SP1 以降で Server Core をサポート), Windows Server 2003 SP2
.NET Framework では、各プラットフォームのすべてのバージョンはサポートしていません。 サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。