Board index » delphi » Focusing invisible controls

Focusing invisible controls

Is there any way that regardless of whether a control is visible or not,
it can be focused? By this I mean is there a method whereby I can
intercept or be notified of a an attempt to focus (such as
Control.SetFocus) a control using WINAPI or VCL messaging?

Our trouble is the system requires a server and client application,
however the server and client apps are being developed by two individual
companies, and the server was designed and finished 2 months ago. As our
company has no access to the source for the server app we have to make
do. The major problem is that we could be displaying a different window
over the string grid control (hence the grid is invisible) when all of a
sudden the server wants the grid to be focused. We have no notification
of this request short of the exception message "cannot focus a disabled
or invisible window". It can happen anywhere and at any time - we have
to be able to make it visible before the request to focus it is

We have no trouble with subclassing the parent form. If it requires
installing an API message hook we'll do it.

Thanks in advance.
Ashley Godfrey.


Re:Focusing invisible controls


you could trap the exception in Application.OnException, of course, but
there is no way to find out at that location *which* was the offending
control. As far as i can see the exception is raised in procedure
TCustomForm.SetActiveControl(Control: TWinControl); You have to find out
how the server event ends up triggering this method. If it calls the grids
SetFocus method, for example, then this would trigger

procedure TWinControl.SetFocus;
  Parent: TCustomForm;
  Parent := GetParentForm(Self);
  if Parent <> nil then

FocusControl calls SetActiveControl. There are no messages involved in this
chain and so nothing to trap. However, SetFocus is a virtual method, so
you could create a descendant of your current grid class and override this
method to make the grid visible before you call the inherited method to do
the actual focusing.

Peter Below (TeamB)

Other Threads