次の方法で共有


Control.Leave イベント

定義

入力フォーカスがコントロールから離れると発生します。

public:
 event EventHandler ^ Leave;
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 プロパティを現在のフォームに設定したりすると、フォーカス イベントは次の順序で発生します。

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

マウスを使用するか、 Focus メソッドを呼び出してフォーカスを変更すると、フォーカス イベントは次の順序で発生します。

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

CausesValidation プロパティが false に設定されている場合、ValidatingイベントとValidated イベントは抑制されます。

EnterイベントとLeave イベントは、Form クラスによって抑制されます。 Form クラスの同等のイベントは、ActivatedイベントとDeactivate イベントです。 EnterイベントとLeave イベントは階層的であり、適切なコントロールに到達するまで親チェーンの上下にカスケードされます。 たとえば、2 つのGroupBox コントロールを持つFormがあり、各GroupBox コントロールに 1 つのTextBox コントロールがあるとします。 キャレットが一方のTextBoxから他方のに移動されると、TextBoxGroupBoxに対してLeaveイベントが発生し、もう一方のGroupBoxTextBoxに対してEnterイベントが発生します。

注意事項

EnterGotFocusLeaveLostFocusValidating、またはValidatedイベント ハンドラー内からフォーカスを設定しないでください。 これを行うと、アプリケーションまたはオペレーティング システムが応答を停止する可能性があります。 詳細については、 WM_KILLFOCUS トピックを参照してください。

イベントの処理の詳細については、「処理とイベントの発生」を参照してください。

適用対象

こちらもご覧ください