Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catch/create OnGetFocus/OnLostFocus events for TCustomControl Delphi

I created a Delphi component inherited from TCustomControl. The component can get focused as inherited from TWinControl, but I need to "highlight" when its get focused and change some properties when it loses the focus. As the Delphi documentation says, TCustomControl have no inherited OnFocus event, so I need to catch the event(?) and implement my own OnGetFocus/OnLostFocus event handlers(?). How can I catch the events when the component get/lose the focus?

like image 849
Walentine Avatar asked Jun 28 '15 16:06

Walentine


1 Answers

The events fired when the control receives or loses the input focus are OnEnter and OnExit and are fired from the DoEnter and DoExit methods which you as a component developer should override:

type
  TMyControl = class(TCustomControl)
  protected
    procedure DoEnter; override;
    procedure DoExit; override;
  end;

implementation

{ TMyControl }

procedure TMyControl.DoEnter;
begin
  inherited;
  // the control received the input focus, so do what you need here; note
  // that it's recommended to call inherited inside this method (which as
  // described in the reference should only fire the OnEnter event now)
end;

procedure TMyControl.DoExit;
begin
  inherited;
  // the control has lost the input focus, so do what you need here; note
  // that it's recommended to call inherited inside this method (which as
  // described in the reference should only fire the OnExit event now)
end;
like image 154
TLama Avatar answered Nov 09 '22 23:11

TLama