[Lazarus] WinControl.CanFocus

Ondrej Pokorny lazarus at kluug.net
Mon Jun 8 19:46:10 CEST 2015


On 08.06.2015 18:47, Martin Frb wrote:
> There is a valid use case
>
>   if HiddenForm.EditFoo.CanFocus then
>     HiddenForm.ActiveControl := HiddenForm.EditFoo;
>   HiddenForm.Show;
>
> This currently works, because of the visibility of the form is not 
> checked.
> (same applies for a form with Enabled := False)
>
> If code prefers setting ActiveControl over calling SetFocus, then the 
> current behaviour makes sense.

That is somehow true. Looking into the LCL sources, I now think it's not 
a good idea to change the current code.

But still it would be nice to have a second CanFocus version available 
in the LCL that could be used with SetFocus.

There are 2 options IMO:

1.) Add an optional parameter to CanFocus. Something like:
function CanFocus(const aConsiderWholeTree: Boolean = False): Boolean;

aConsiderWholeTree = False: current code
aConsiderWholeTree = True: check form as well

2.) Add a new CanSetFocus (or whatever name) function to TWinControl:
function TWinControl.CanSetFocus: Boolean;

that checks the form as well and prevents SetFocus exceptions.

----
I think the second is better, what do you think? Or do you think that 
CanSetFocus/CanFocus(aConsiderWholeTree) shouldn't be in the LCL at all?

Ondrej




More information about the Lazarus mailing list