Control.LostFocus 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當控制失焦時會發生。
public:
event EventHandler ^ LostFocus;
[System.ComponentModel.Browsable(false)]
public event EventHandler LostFocus;
[System.ComponentModel.Browsable(false)]
public event EventHandler? LostFocus;
[<System.ComponentModel.Browsable(false)>]
member this.LostFocus : EventHandler
Public Custom Event LostFocus As EventHandler
事件類型
- 屬性
範例
以下程式碼範例示範如何驗證 TextBox1 的文字。 它也示範如何處理 LostFocus 事件,方法是將屬性設定 FileDialog.InitialDirectory 為 TextBox1 中的文字。 程式碼範例使用該 ErrorProvider.GetError 方法在開啟檔案對話框前檢查錯誤。 要執行此範例,將以下程式碼貼入包含 TextBox 、 、 OpenFileDialogOpenFileDialog1、 ButtonButton1ErrorProvider 和 。ErrorProvider1TextBox1 確保所有事件都與其事件處理者相關聯。
private:
void TextBox1_Validating( Object^ sender,
System::ComponentModel::CancelEventArgs^ e )
{
// If nothing is entered,
// an ArgumentException is caught; if an invalid directory is entered,
// a DirectoryNotFoundException is caught. An appropriate error message
// is displayed in either case.
try
{
System::IO::DirectoryInfo^ directory = gcnew System::IO::DirectoryInfo( TextBox1->Text );
directory->GetFiles();
ErrorProvider1->SetError( TextBox1, "" );
}
catch ( System::ArgumentException^ )
{
ErrorProvider1->SetError( TextBox1, "Please enter a directory" );
}
catch ( System::IO::DirectoryNotFoundException^ )
{
ErrorProvider1->SetError( TextBox1, "The directory does not exist."
"Try again with a different directory." );
}
}
// This method handles the LostFocus event for TextBox1 by setting the
// dialog's InitialDirectory property to the text in TextBox1.
void TextBox1_LostFocus( Object^ sender, System::EventArgs^ e )
{
OpenFileDialog1->InitialDirectory = TextBox1->Text;
}
// This method demonstrates using the ErrorProvider.GetError method
// to check for an error before opening the dialog box.
void Button1_Click( System::Object^ sender, System::EventArgs^ e )
{
//If there is no error, then open the dialog box.
if ( ErrorProvider1->GetError( TextBox1 )->Equals( "" ) )
{
::DialogResult dialogResult = OpenFileDialog1->ShowDialog();
}
}
private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
// If nothing is entered,
// an ArgumentException is caught; if an invalid directory is entered,
// a DirectoryNotFoundException is caught. An appropriate error message
// is displayed in either case.
try
{
System.IO.DirectoryInfo directory =
new System.IO.DirectoryInfo(textBox1.Text);
directory.GetFiles();
errorProvider1.SetError(textBox1, "");
}
catch(System.ArgumentException ex1)
{
errorProvider1.SetError(textBox1, "Please enter a directory");
}
catch(System.IO.DirectoryNotFoundException ex2)
{
errorProvider1.SetError(textBox1, "The directory does not exist." +
"Try again with a different directory.");
}
}
// This method handles the LostFocus event for textBox1 by setting the
// dialog's InitialDirectory property to the text in textBox1.
private void textBox1_LostFocus(object sender, System.EventArgs e)
{
openFileDialog1.InitialDirectory = textBox1.Text;
}
// This method demonstrates using the ErrorProvider.GetError method
// to check for an error before opening the dialog box.
private void button1_Click(System.Object sender, System.EventArgs e)
{
//If there is no error, then open the dialog box.
if (errorProvider1.GetError(textBox1)=="")
{
DialogResult dialogResult = openFileDialog1.ShowDialog();
}
}
Private Sub TextBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
' If nothing is entered,
' an ArgumentException is caught; if an invalid directory is entered,
' a DirectoryNotFoundException is caught. An appropriate error message
' is displayed in either case.
Try
Dim directory As New System.IO.DirectoryInfo(TextBox1.Text)
directory.GetFiles()
ErrorProvider1.SetError(TextBox1, "")
Catch ex1 As System.ArgumentException
ErrorProvider1.SetError(TextBox1, "Please enter a directory")
Catch ex2 As System.IO.DirectoryNotFoundException
ErrorProvider1.SetError(TextBox1, _
"The directory does not exist." & _
"Try again with a different directory.")
End Try
End Sub
' This method handles the LostFocus event for TextBox1 by setting the
' dialog's InitialDirectory property to the text in TextBox1.
Private Sub TextBox1_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles TextBox1.LostFocus
OpenFileDialog1.InitialDirectory = TextBox1.Text
End Sub
' This method demonstrates using the ErrorProvider.GetError method
' to check for an error before opening the dialog box.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'If there is no error, then open the dialog box.
If ErrorProvider1.GetError(TextBox1) = "" Then
Dim dialogResult As DialogResult = OpenFileDialog1.ShowDialog()
End If
End Sub
備註
當你用鍵盤(TAB、SHIFT+TAB 等)、呼叫 Select or SelectNextControl 方法,或將屬性設 ContainerControl.ActiveControl 為當前形式來更改焦點時,焦點事件會依以下順序發生:
當你用滑鼠或呼叫 Focus 方法改變焦點時,焦點事件會依以下順序發生:
若 CausesValidation 屬性設為 false,則 Validating 與 Validated 事件會被抑制。
如果 Cancel 的 CancelEventArgs 屬性設定為 true 事件委托中 Validating ,所有通常會在事件後 Validating 發生的事件都會被抑制。
備註
和GotFocusLostFocus事件是低階焦點事件,與 WM_KILLFOCUS 和 WM_SETFOCUS Windows 訊息相關聯。 通常, GotFocus 和 LostFocus 事件僅在更新 UICues 或撰寫自訂控制時使用。 相反地Enter,除了類別外,所有控制ActivatedForm都應該使用 and LeaveDeactivate 事件。 欲了解更多相關 GotFocus 資訊及 LostFocus 活動資訊,請參閱 WM_KILLFOCUS 及 WM_KILLFOCUS 主題。
謹慎
不要試圖從 、 GotFocus、 Leave、 LostFocusValidatingValidated 、 或事件處理者中Enter來設定焦點。 這樣做可能會導致你的應用程式或作業系統停止回應。 欲了解更多資訊,請參閱 WM_KILLFOCUS 主題。
如需處理事件的詳細資訊,請參閱 處理和引發事件。