Control.Enter イベント

定義

コントロールが入力されると発生します。

public:
 event EventHandler ^ Enter;
public event EventHandler Enter;
public event EventHandler? Enter;
member this.Enter : EventHandler 
Public Custom Event Enter As EventHandler 

イベントの種類

次のコード例では、 イベントを Enter 使用して、特定の条件下で の前景色と背景色を TextBox 変更します。

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 など) を使用してフォーカスを変更したり、 メソッドや SelectNextControl メソッドを呼び出Selectしたり、 プロパティを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

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

Note

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

注意事項

、または イベント ハンドラー内EnterGotFocusLostFocusLeaveValidatingからフォーカスを設定しないでください。Validated これにより、アプリケーションまたはオペレーティング システムが応答を停止する可能性があります。 詳細については、「 WM_KILLFOCUS キーボード入力リファレンス」セクションのトピックと、「メッセージ とメッセージ キューについて 」トピックの「メッセージ デッドロック」セクションを参照してください。

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

適用対象

こちらもご覧ください