Hello,
Sorry for possibly stupid subject and maybe off-topic question. In my UIA code, I am testing if a control has focus. So I have a FControl: TControl declared in my class.
Now say FControl contains a TForm1 instance, simply we have very simple program with just an empty window. Then an expression: Result := FControl is TWinControl and TWinControl(FControl).Focused; returns false. I am sure the form has focus..., because ShowMessage(BoolToStr(Focused)) returns -1, which represents true...
Mailing lists / lazarus / 2018-03
An expression is returning false, why?
6 messages · 4 people · –
- LLubos Pintes 3
- GGiuliano Colla 1
- VVojtěch Čihák 1
- Zzeljko 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi,
I just tested. Active Form always returns Focused False. Use property Active instead.
Result := (FControl is TCustomForm) and TCustomForm(FControl).Active;
V.
______________________________________________________________
Show quoted text 5 lines
Hello,
Sorry for possibly stupid subject and maybe off-topic question. In my UIA code, I am testing if a control has focus. So I have a FControl: TControl declared in my class.
Now say FControl contains a TForm1 instance, simply we have very simple program with just an empty window. Then an expression: Result := FControl is TWinControl and TWinControl(FControl).Focused; returns false. I am sure the form has focus..., because ShowMessage(BoolToStr(Focused)) returns -1, which represents true...
Hello and thank for a reply. Do I understand correctly that a form is a special case?
What about TEdit or TButton for example? I am encapsulating a TControl instance and am asking for properties needed for UI Automation, occasionally asking if the encapsulated control is a TWinControl instance. I tested the simplest LCL program, just an empty form, and it really caused me headache. I just forcefully set the HasKeyboardFocus property to true for testing and the form was reported correctly. Dňa 04.03.2018 o 20:44 Vojtěch Čihák via Lazarus napísal(a):
Show quoted text 33 lines
Lubos Pintes wrote 8 lines
Use MyForm.ActiveControl to get focused control on active form.
zeljko
Hello and thank. And in general, if I have a TControl instance, can you tell me a reliable algorithm that tells me if the control has a focus? Only if it makes sense of course. So probably something like I tried before, that is the control is a TWinControl instance and the Focused property is true?
I know it doesn't work for a form itself. Maybe this is a bug? Dňa 05.03.2018 o 8:53 zeljko via Lazarus napísal(a):
Show quoted text 14 lines
Il 05/03/2018 09:41, Lubos Pintes via Lazarus ha scritto:
Hello and thank. And in general, if I have a TControl instance, can you tell me a reliable algorithm that tells me if the control has a focus?
By definition, the focused control is the control which receives keyboard input.
That's why the form in itself is never focused because a form doesn't handle keyboard input.
With just a form you may strike any key, and nothing will happen: the keyboard input is simply ignored.
The Form's ActiveControl property tells you which control has focus and will receive the keyboard input.
The control's Focused property tells you if the control has focus.
The focused control may be a button a TEdit, or whatever control which may perform an action upon striking a key.
You may push a focused button by striking the enter key or the space bar, you may enter text on a Tedit, etc.
Giuliano
