Control.Leave 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當輸入焦點離開控制器時會發生。
public:
event EventHandler ^ Leave;
public event EventHandler Leave;
public event EventHandler? Leave;
member this.Leave : EventHandler
Public Custom Event Leave As EventHandler
事件類型
範例
以下程式碼範例利用事件 Leave 將控制項重置至先前狀態。
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 等)、呼叫 Select or SelectNextControl 方法,或將屬性設 ContainerControl.ActiveControl 為當前形式來更改焦點時,焦點事件會依以下順序發生:
當你用滑鼠或呼叫 Focus 方法改變焦點時,焦點事件會依以下順序發生:
若 CausesValidation 屬性設為 false,則 Validating 與 Validated 事件會被抑制。
備註
Enter該Leave事件被班級壓Form制。 該類別中 Form 對應的事件是 和 ActivatedDeactivate 事件。 Enter和Leave事件是階層式的,會在母鏈上上下層遞減,直到達到適當的控制。 舉例來說,假設你有一個 Form 有兩個 GroupBox 控制點,每個 GroupBox 控制點都有一個 TextBox 控制點。 當插入點從一個TextBox移到另一個時,Leave事件會被提升到TextBoxEnterGroupBox另一個和,事件也會被提升到另一個GroupBoxTextBox和。
謹慎
不要試圖從 、 GotFocus、 Leave、 LostFocusValidatingValidated 、 或事件處理者中Enter來設定焦點。 這樣做可能會導致你的應用程式或作業系統停止回應。 欲了解更多資訊,請參閱 WM_KILLFOCUS 主題。
如需處理事件的詳細資訊,請參閱 處理和引發事件。