Control.Leave イベント

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

Public Event Leave As EventHandler
[C#]
public event EventHandler Leave;
[C++]
public: __event EventHandler* Leave;

[JScript] JScript では、このクラスで定義されているイベントを処理できます。ただし、独自に定義することはできません。

イベント データ

イベント ハンドラが EventArgs 型の引数を受け取りました。

解説

キーボード (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 コントロールを持っているとします。カレット (^) が 1 つの TextBox からもう 1 つのテキスト ボックスに移動すると、 TextBox および GroupBox に対して Leave イベントが発生し、他方の GroupBox および TextBox に対して Enter イベントが発生します。

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

使用例

 
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 'textBox1_Enter
   
   
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 'textBox1_Leave
End Class 'Form1 

[C#] 
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);
    }
}

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);
}

[C++] 
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);
   }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Control クラス | Control メンバ | System.Windows.Forms 名前空間 | OnLeave