Control.Leave イベント
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
入力フォーカスがコントロールから離れると発生します。
public:
event EventHandler ^ Leave;
public event EventHandler Leave;
member this.Leave : EventHandler
Public Custom Event Leave As EventHandler
イベントの種類
例
次のコード例では、 Leave イベントを使用して、コントロールを以前の状態にリセットします。
private:
void textBox1_Enter( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// If the TextBox contains text, change its foreground and background colors.
if ( textBox1->Text != String::Empty )
{
textBox1->ForeColor = Color::Red;
textBox1->BackColor = Color::Black;
// Move the selection pointer to the end of the text of the control.
textBox1->Select(textBox1->Text->Length,0);
}
}
void textBox1_Leave( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Reset the colors and selection of the TextBox after focus is lost.
textBox1->ForeColor = Color::Black;
textBox1->BackColor = Color::White;
textBox1->Select(0,0);
}
private void textBox1_Enter(object sender, System.EventArgs e)
{
// If the TextBox contains text, change its foreground and background colors.
if (!string.IsNullOrEmpty(textBox1.Text))
{
textBox1.ForeColor = Color.Red;
textBox1.BackColor = Color.Black;
// Move the selection pointer to the end of the text of the control.
textBox1.Select(textBox1.Text.Length, 0);
}
}
private void textBox1_Leave(object sender, System.EventArgs e)
{
// Reset the colors and selection of the TextBox after focus is lost.
textBox1.ForeColor = Color.Black;
textBox1.BackColor = Color.White;
textBox1.Select(0,0);
}
Private Sub textBox1_Enter(sender As Object, e As System.EventArgs) Handles textBox1.Enter
' If the TextBox contains text, change its foreground and background colors.
If textBox1.Text <> [String].Empty Then
textBox1.ForeColor = Color.Red
textBox1.BackColor = Color.Black
' Move the selection pointer to the end of the text of the control.
textBox1.Select(textBox1.Text.Length, 0)
End If
End Sub
Private Sub textBox1_Leave(sender As Object, e As System.EventArgs) Handles textBox1.Leave
' Reset the colors and selection of the TextBox after focus is lost.
textBox1.ForeColor = Color.Black
textBox1.BackColor = Color.White
textBox1.Select(0, 0)
End Sub
End Class
注釈
キーボード (TAB、Shift + Tab など) を使用してフォーカスを変更したり、 Select メソッドや SelectNextControl メソッドを呼び出したり、 ContainerControl.ActiveControl プロパティを現在のフォームに設定したりすると、フォーカス イベントは次の順序で発生します。
マウスを使用するか、 Focus メソッドを呼び出してフォーカスを変更すると、フォーカス イベントは次の順序で発生します。
CausesValidation プロパティが false に設定されている場合、ValidatingイベントとValidated イベントは抑制されます。
Note
EnterイベントとLeave イベントは、Form クラスによって抑制されます。 Form クラスの同等のイベントは、ActivatedイベントとDeactivate イベントです。 EnterイベントとLeave イベントは階層的であり、適切なコントロールに到達するまで親チェーンの上下にカスケードされます。 たとえば、2 つのForm コントロールを持つGroupBoxがあり、各GroupBox コントロールに 1 つのTextBox コントロールがあるとします。 キャレットが一方のTextBoxから他方のに移動されると、LeaveとTextBoxに対してGroupBoxイベントが発生し、もう一方のEnterとGroupBoxに対してTextBoxイベントが発生します。
Caution
Enter、GotFocus、Leave、LostFocus、Validating、またはValidatedイベント ハンドラー内からフォーカスを設定しないでください。 これを行うと、アプリケーションまたはオペレーティング システムが応答を停止する可能性があります。 詳細については、 WM_KILLFOCUS トピックを参照してください。
イベントの処理の詳細については、「処理とイベントの発生」を参照してください。