この記事は翻訳者によって翻訳されたものです。 記事の文章にポインターを重ねると、原文のテキストが表示されます。 |
訳文
原文
|
Control.LostFocus イベント
コントロールにフォーカスがなくなると発生します。
アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)
キーボード (Tab、Shift + Tab など) を使用するか、Select メソッドまたは SelectNextControl メソッドを呼び出すか、ContainerControl.ActiveControl プロパティを現在のフォームに設定してフォーカスを変更するとき、次の順序でフォーカス イベントが発生します。
-
LostFocus
マウスを使用するか Focus メソッドを呼び出してフォーカスを変更するとき、フォーカス イベントは次の順序で発生します。
-
LostFocus
CausesValidation プロパティが false に設定されている場合、Validating イベントおよび Validated イベントは発生しません。
Validating イベント デリゲートで CancelEventArgs の Cancel プロパティが true に設定されると、通常は Validating イベントの後に発生するすべてのイベントが発生しなくなります。
メモ
|
|---|
|
GotFocus イベントおよび LostFocus イベントは、WM_KILLFOCUS Windows メッセージおよび WM_SETFOCUS Windows メッセージに結び付けられた、下位のフォーカス イベントです。 一般的に、GotFocus イベントおよび LostFocus イベントは、UICues を更新するとき、またはカスタム コントロールを作成するときにだけ使用されます。 代わりに、Activated イベントおよび Deactivate イベントを使用する Form クラスを除くすべてのコントロールに、Enter イベントおよび Leave イベントを使用する必要があります。 GotFocus および LostFocus イベントの詳細については、MSDN ライブラリ (http://msdn.microsoft.com/ja-jp/library/default.aspx) にある「Keyboard Input Reference (キーボード入力リファレンス)」セクションの WM_SETFOCUS および WM_KILLFOCUS のトピックを参照してください。 |
注意
|
|---|
|
Enter、GotFocus、Leave、LostFocus、Validating、または Validated イベント ハンドラーの中からフォーカスを設定しないでください。 フォーカスを設定すると、アプリケーションやオペレーティング システムが応答を停止することがあります。 詳細については、MSDN ライブラリ (http://msdn.microsoft.com/ja-jp/library/default.aspx) にある「Keyboard Input Reference (キーボード入力リファレンス)」セクションの WM_KILLFOCUS、および「About Messages and Message Queues (メッセージおよびメッセージ キューについて)」トピックの「Message Deadlocks (メッセージ デッドロック)」セクションを参照してください。 |
イベント処理の詳細については、「イベントの利用」を参照してください。
TextBox1 のテキストを検証する方法を次のコード例に示します。 また、FileDialog.InitialDirectory プロパティを TextBox1 のテキストに設定して、LostFocus イベントを処理する方法も示します。 このコード例では、ErrorProvider.GetError メソッドを使用して、ファイル ダイアログ ボックスを開く前にエラーがあるかどうかを調べます。 この例を実行するには、TextBox1 という名前の TextBox、OpenFileDialog1 という名前の OpenFileDialog、Button1 という名前の Button、および ErrorProvider1 という名前の ErrorProvider が配置されているフォームに次のコードを貼り付けます。 必ずすべてのイベントをイベント ハンドラーに関連付けるようにしてください。
private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e) { // If nothing is entered, // an ArgumentException is caught; if an invalid directory is entered, // a DirectoryNotFoundException is caught. An appropriate error message // is displayed in either case. try { System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(textBox1.Text); directory.GetFiles(); errorProvider1.SetError(textBox1, ""); } catch(System.ArgumentException ex1) { errorProvider1.SetError(textBox1, "Please enter a directory"); } catch(System.IO.DirectoryNotFoundException ex2) { errorProvider1.SetError(textBox1, "The directory does not exist." + "Try again with a different directory."); } } // This method handles the LostFocus event for textBox1 by setting the // dialog's InitialDirectory property to the text in textBox1. private void textBox1_LostFocus(object sender, System.EventArgs e) { openFileDialog1.InitialDirectory = textBox1.Text; } // This method demonstrates using the ErrorProvider.GetError method // to check for an error before opening the dialog box. private void button1_Click(System.Object sender, System.EventArgs e) { //If there is no error, then open the dialog box. if (errorProvider1.GetError(textBox1)=="") { DialogResult dialogResult = openFileDialog1.ShowDialog(); } }
The following code example demonstrates validating the TextBox1 object's text. It also demonstrates handling the text box’s LostFocus event by setting the file dialog's InitialDirectory property to the text in TextBox1. The code example used the ErrorProvider.GetError method to check for an error before opening the file dialog. To run this example paste the following code into a form containing a TextBox named TextBox1, an OpenFileDialog named OpenFileDialog1, a button named Button1, and an ErrorProvider named ErrorProvider1.
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 システム要件」を参照してください。
メモ
注意