Control.Enter 事件

定義

發生於輸入控制項時。

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

事件類型

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 事件。

注意

類別 EnterForm 隱藏 和 Leave 事件。 類別中的 Form 對等事件是 ActivatedDeactivate 事件。 EnterLeave 事件是階層式事件,會向上和向下串聯父鏈結,直到到達適當的控制項為止。 例如,假設您有具有兩 GroupBoxForm 控制項的 ,而且每個 GroupBox 控制項都有一個 TextBox 控制項。 當插入號從一個移到另一個 TextBox 插入號時, Leave 會針對 TextBoxGroupBox 引發 事件,而 Enter 另一 GroupBox 個 和 TextBox 會引發 事件。

警告

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

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

適用於

另請參閱