RAD Studio VCL Reference
|
Occurs when a control receives the input focus.
property OnEnter: TNotifyEvent;
__property TNotifyEvent OnEnter;
Use the OnEnter event handler to cause any special processing to occur when a control becomes active.
The OnEnter event does not occur when switching between forms or between another application and the application that includes the control.
When switching between controls in separate container controls such as the TPanel and the TGroupBox controls, an OnEnter event occurs for the container before the OnEnter event of the contained control.
Similarly, an OnExit event of the container occurs after the OnExit event of the control in a container when focus moves to another control outside the container.
For example, consider a form with an OK button and a group box that contains three radio buttons, where focus is currently on the OK button. When the user clicks one of the radio buttons, an OnExit event of the button occurs, followed by an OnEnter event on the group box, and finally an OnEnter event on the radio button that was clicked. If the user then clicks on the OK button, an OnExit event for the radio button occurs followed by an OnExit event for the group box, and then the button's OnEnter event occurs.
C++ Examples:
/* This example takes an edit control, a button, and a rich edit control on a form. When the user presses the button, a Font dialog appears. When the user clicks the Apply (not OK) button in the Font dialog, the currently selected font is applied to the active control. Clicking the button sets the ActiveControl to the button. That is why we need to save the ActiveControl in a shared OnEnter procedure. In the rich edit, only the text selected converts. */ TWinControl *myActiveControl; void __fastcall TForm1::Button1Click(TObject *Sender) { FontDialog1->Options << fdApplyButton; FontDialog1->Execute(); } void __fastcall TForm1::FontDialog1Apply(TObject *Sender, HWND Wnd) { if (myActiveControl->ClassNameIs("TEdit")) (dynamic_cast<TEdit *>(myActiveControl))->Font->Assign(FontDialog1->Font); else if (myActiveControl->ClassNameIs("TRichEdit")) (dynamic_cast<TRichEdit *>(myActiveControl))->SelAttributes->Assign(FontDialog1->Font); else MessageBeep(0); } void __fastcall TForm1::Edit1Enter(TObject *Sender) { myActiveControl = ActiveControl; }
/* This example uses an edit box and a memo control on a form. When either the edit box or the memo is the active control, it is colored yellow. When the active control becomes inactive, the color of the control returns to the Windows system color for a window. These event handlers could also be shared. */ void __fastcall TForm1::Edit1Enter(TObject *Sender) { Edit1->Color = clYellow; } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit1Exit(TObject *Sender) { Edit1->Color = clWindow; } //--------------------------------------------------------------------------- void __fastcall TForm1::Memo1Enter(TObject *Sender) { Memo1->Color = clYellow; } //--------------------------------------------------------------------------- void __fastcall TForm1::Memo1Exit(TObject *Sender) { Memo1->Color = clWindow; }
Delphi Examples:
{ This example takes an edit control, a button, and a rich edit control on a form. When the user presses the button, a Font dialog appears. When the user clicks the Apply (not OK) button in the Font dialog, the currently selected font is applied to the active control. Clicking the button sets the ActiveControl to the button. That is why we need to save the ActiveControl in a shared OnEnter procedure. In the rich edit, only the text selected converts. } procedure TForm1.Button1Click(Sender: TObject); begin FontDialog1.Options := FontDialog1.Options + [fdApplyButton]; FontDialog1.Execute; end; procedure TForm1.Edit1Enter(Sender: TObject); begin myActiveControl := ActiveControl; end; procedure TForm1.FontDialog1Apply(Sender: TObject; Wnd: HWND); begin if myActiveControl is TEdit then with myActiveControl as TEdit do Font.Assign(TFontDialog(Sender).Font) else if myActiveControl is TRichEdit then with myActiveControl as TRichEdit do SelAttributes.Assign(TFontDialog(Sender).Font) else Beep; end;
{ This example uses an edit box and a memo control on a form. When either the edit box or the memo is the active control, it is colored yellow. When the active control becomes inactive, the color of the control returns to the Windows system color for a window. These event handlers could also be shared. } procedure TForm1.Edit1Enter(Sender: TObject); begin Edit1.Color := clYellow; end; procedure TForm1.Edit1Exit(Sender: TObject); begin Edit1.Color := clWindow; end; procedure TForm1.Memo1Enter(Sender: TObject); begin Memo1.Color := clYellow; end; procedure TForm1.Memo1Exit(Sender: TObject); begin Memo1.Color := clWindow; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|