共用方式為


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 等) 、呼叫 SelectSelectNextControl 方法,或將 屬性設定 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類別會隱藏 FormLeave 事件。 類別中的 Form 對等事件是 ActivatedDeactivate 事件。 EnterLeave 事件是階層式的,而且會向上和向下串聯父鏈結,直到到達適當的控制項為止。 例如,假設您有兩個 Form 控制項的 GroupBox ,而每個控制項都有 GroupBox 一個 TextBox 控制項。 當插入號從其中一個移到另一個插入號時, Leave 會針對 TextBoxGroupBox 引發 事件,並 Enter 針對另一 TextBoxGroupBox 個 和 TextBox 引發 事件。

警告

請勿嘗試從 、、、、 ValidatingLostFocusValidated 事件處理常式內 Enter 設定焦點。 LeaveGotFocus 這樣做可能會導致您的應用程式或作業系統停止回應。 如需詳細資訊,請參閱 WM_KILLFOCUS 關於 訊息和訊息佇列 主題的一節和一節中的主題。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱