Control.Enter 事件

定義

發生於輸入控制項時。Occurs when the control is entered.

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

範例

下列程式碼範例使用 Enter 事件,在特定條件下變更 TextBox 的前景和背景色彩。The following code example uses the Enter event to change the foreground and background colors of a TextBox under particular conditions.

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 屬性設定為目前的表單,焦點事件會依照下列順序發生:When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

當您使用滑鼠或藉由呼叫 Focus 方法來變更焦點時,焦點事件會依照下列順序發生:When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

如果 CausesValidation 屬性設為 false,則會隱藏 ValidatingValidated 事件。If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

注意

EnterLeave 事件會由 Form 類別隱藏。The Enter and Leave events are suppressed by the Form class. Form 類別中的對等事件是 ActivatedDeactivate 事件。The equivalent events in the Form class are the Activated and Deactivate events. EnterLeave 事件都是階層式的,並且會在父代鏈上向上和向下 cascade,直到達到適當的控制項為止。The Enter and Leave events are hierarchical and will cascade up and down the parent chain until the appropriate control is reached. 例如,假設您有兩個 GroupBox 控制項的 Form,而且每個 GroupBox 控制項都有一個 TextBox 控制項。For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. 當插入號從一個 TextBox 移至另一個時,會針對 TextBoxGroupBox 引發 Leave 事件,並針對另一個 GroupBoxTextBox 引發 Enter 事件。When the caret is moved from one TextBox to the other, the Leave event is raised for the TextBox and GroupBox, and the Enter event is raised for the other GroupBox and TextBox.

警告

請勿嘗試從 EnterGotFocusLeaveLostFocusValidatingValidated 事件處理常式中設定焦點。Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. 這麼做可能會導致您的應用程式或作業系統停止回應。Doing so can cause your application or the operating system to stop responding. 如需詳細資訊,請參閱關於訊息和訊息佇列主題的「鍵盤輸入參考」一節中的 WM_KILLFOCUS 主題和「訊息鎖死」一節。For more information, see the WM_KILLFOCUS topic in the "Keyboard Input Reference" section and the "Message Deadlocks" section of the About Messages and Message Queues topic.

如需處理事件的詳細資訊,請參閱處理和引發事件For more information about handling events, see Handling and Raising Events.

適用於

另請參閱