Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
General
»
SetFocus fails
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Poll: Watches and Display...
by
Martin_fr
[
Today
at 12:08:33 am]
Free AI to use in local
by
gidesa
[March 29, 2024, 10:14:48 pm]
Program loop "runaway" wh...
by
Thaddy
[March 29, 2024, 09:34:48 pm]
A "leisure" question on W...
by
EventHorizon
[March 29, 2024, 09:29:50 pm]
Unable to open the packag...
by
LazKuri
[March 29, 2024, 08:42:25 pm]
Your best UI design - con...
by
mgc
[March 29, 2024, 08:33:26 pm]
Slight improvement for la...
by
n7800
[March 29, 2024, 07:18:44 pm]
Controlling the behavior ...
by
1HuntnMan
[March 29, 2024, 04:55:21 pm]
Making panels with lots o...
by
lainz
[March 29, 2024, 04:00:20 pm]
Ribbon bar in Lazarus 3.2...
by
lainz
[March 29, 2024, 03:58:42 pm]
Have anyone develop UEFI ...
by
PascalDragon
[March 29, 2024, 02:16:40 pm]
Dynarray initialization p...
by
PascalDragon
[March 29, 2024, 02:12:47 pm]
STARS and Regular POLYGON...
by
Boleeman
[March 29, 2024, 02:05:38 pm]
how to change the color f...
by
cdbc
[March 29, 2024, 12:02:27 pm]
Understanding and adjusti...
by
ArchChem
[March 29, 2024, 11:49:35 am]
Offical launch of the 1 B...
by
abouchez
[March 29, 2024, 11:14:35 am]
Using KeyPress event, how...
by
KodeZwerg
[March 29, 2024, 10:44:31 am]
Big Numbers Math
by
Thaddy
[March 29, 2024, 07:22:49 am]
Parquet?
by
Thaddy
[March 29, 2024, 07:11:17 am]
Project fails on run but ...
by
jamie
[March 29, 2024, 12:54:49 am]
Curling Pythagoras Tree: ...
by
Boleeman
[March 29, 2024, 12:00:37 am]
how to search in rxmemory...
by
rcmz
[March 28, 2024, 10:19:54 pm]
Configure JCF messages
by
GordonFindlay
[March 28, 2024, 10:16:19 pm]
Dialog ShowModal and an i...
by
d7_2_laz
[March 28, 2024, 09:51:39 pm]
JEDI error but unit compi...
by
Thaddy
[March 28, 2024, 09:30:33 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: SetFocus fails (Read 6021 times)
dietmar
Full Member
Posts: 170
SetFocus fails
«
on:
September 11, 2017, 10:14:02 pm »
Hi,
I have a form where a want to set the focus on a button via code.
dlgCustom2.Button1.SetFocus
raises exception:"Can not focus"
What can be the reason for this and how can it be fixed?
Thx,
Dietmar
Logged
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)
Thaddy
Hero Member
Posts: 14221
Probably until I exterminate Putin.
Re: SetFocus fails
«
Reply #1 on:
September 11, 2017, 10:21:32 pm »
dlgCustom2.Button1.Focused := true;
In that case, once the form itself gets focused...... (is it?) the button will have the focus.
Otherwise you need to ensure that the form that owns the button has focus first...
Logged
Specialize a type, not a var.
dietmar
Full Member
Posts: 170
Re: SetFocus fails
«
Reply #2 on:
September 11, 2017, 10:39:09 pm »
Setting the focus to the form before setting it to the button creates the same error.
Logged
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)
PatBayford
Full Member
Posts: 125
Re: SetFocus fails
«
Reply #3 on:
September 11, 2017, 11:08:26 pm »
Where are you calling SetFocus from? You will need to ensure that BOTH the Form and the Button exist BEFORE you call SetFocus.
Logged
Lazarus 1.8.0 FPC 3.0.2 SVN 56594 Windows 10 64bit (i386-win32-win32/win64)
RAW
Hero Member
Posts: 868
Re: SetFocus fails
«
Reply #4 on:
September 11, 2017, 11:12:48 pm »
Normally this occurs if someone tries to set the focus in the OnCreate event for example.... So you need to set the focus if the form or the dialog is ready... (OnShow).
Logged
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).
dietmar
Full Member
Posts: 170
Re: SetFocus fails
«
Reply #5 on:
September 11, 2017, 11:33:53 pm »
The SetFocus is called after Create.
The form is openend correctly and displays the button (if I omit the SetFocus).
Setting the focus after the ShowModal doesn't change anything.
Dietmar
Logged
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)
RAW
Hero Member
Posts: 868
Re: SetFocus fails
«
Reply #6 on:
September 12, 2017, 12:10:39 am »
Try this...
Code: Pascal
[Select]
[+]
[-]
btn
.
TabOrder
:
=
0
;
Logged
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).
ASerge
Hero Member
Posts: 2223
Re: SetFocus fails
«
Reply #7 on:
September 12, 2017, 01:00:25 am »
ActiveControl := ...
Unfortunately in the Lazarus documentation this property is not explained. You can use the
Delphi documentation
, because his appointment is the same in Lazarus.
Logged
RAW
Hero Member
Posts: 868
Re: SetFocus fails
«
Reply #8 on:
September 12, 2017, 02:37:11 am »
Really ??? How do you use this @ASerge... ?
Code: Pascal
[Select]
[+]
[-]
Procedure
TForm1
.
Button1Click
(
Sender
:
TObject
)
;
Var
labInfo
:
TLabel
;
memo
:
TMemo
;
btn
:
TButton
;
wndNew
:
TForm
;
Begin
wndNew
:
=
TForm
.
Create
(
Self
)
;
Try
wndNew
.
Caption
:
=
'MyNewWindow'
;
wndNew
.
Constraints
.
MinHeight
:
=
350
;
wndNew
.
Constraints
.
MinWidth
:
=
400
;
wndNew
.
DoubleBuffered
:
=
True
;
wndNew
.
SetBounds
(
(
Screen
.
WorkAreaWidth
-
700
)
Div
2
,
(
Screen
.
WorkAreaHeight
-
500
)
Div
2
,
700
,
500
)
;
memo
:
=
TMemo
.
Create
(
Self
)
;
memo
.
Align
:
=
alTop
;
memo
.
Height
:
=
200
;
memo
.
Text
:
=
'Type in whatever you like...'
;
memo
.
WordWrap
:
=
True
;
memo
.
Font
.
Quality
:
=
fqAntialiased
;
memo
.
Parent
:
=
wndNew
;
labInfo
:
=
TLabel
.
Create
(
Self
)
;
labInfo
.
Caption
:
=
'Hello Everybody...'
;
labInfo
.
Align
:
=
alTop
;
labInfo
.
Font
.
Size
:
=
15
;
labInfo
.
Font
.
Quality
:
=
fqAntialiased
;
labInfo
.
AutoSize
:
=
True
;
labInfo
.
Parent
:
=
wndNew
;
btn
:
=
TButton
.
Create
(
Self
)
;
btn
.
Caption
:
=
'I am a button...'
;
btn
.
Font
.
Quality
:
=
fqAntialiased
;
btn
.
Font
.
Size
:
=
labInfo
.
Font
.
Size
;
btn
.
Align
:
=
alBottom
;
btn
.
Parent
:
=
wndNew
;
btn
.
TabOrder
:
=
0
;
//works nice !!!
btn
.
AutoSize
:
=
True
;
//ActiveControl:= btn; Error !!!
wndNew
.
ShowModal
;
Finally
wndNew
.
Release
;
wndNew
:
=
Nil
;
End
;
End
;
Logged
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).
PatBayford
Full Member
Posts: 125
Re: SetFocus fails
«
Reply #9 on:
September 12, 2017, 04:57:44 am »
Code: Pascal
[Select]
[+]
[-]
btn
.
AutoSize
:
=
True
;
//ActiveControl:= btn; Error !!!
wndNew
.
ShowModal
;
Try using wndwNew.ActiveControl := btn;
Logged
Lazarus 1.8.0 FPC 3.0.2 SVN 56594 Windows 10 64bit (i386-win32-win32/win64)
Zaxxon
New Member
Posts: 30
Re: SetFocus fails
«
Reply #10 on:
September 12, 2017, 08:17:11 am »
Try
Code: Pascal
[Select]
[+]
[-]
if
dlgCustom2
.
Button1
.
Visible
then
dlgCustom2
.
Button1
.
SetFocus
;
Logged
RAW
Hero Member
Posts: 868
Re: SetFocus fails
«
Reply #11 on:
September 12, 2017, 01:01:42 pm »
@PatBayford: Thanks, yes this is working fine:
Code: Pascal
[Select]
[+]
[-]
wndNew
.
ActiveControl
:
=
btn
;
@Zaxxon: Visible and SetFocus doesn't work for me...
Logged
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).
dietmar
Full Member
Posts: 170
Re: SetFocus fails
«
Reply #12 on:
September 12, 2017, 04:30:14 pm »
Thank you all.
Finally, the only one that worked for me was the idea with TabOrder.
Dietmar
Logged
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)
ASerge
Hero Member
Posts: 2223
Re: SetFocus fails
«
Reply #13 on:
September 15, 2017, 01:44:39 pm »
Quote from: dietmar on September 12, 2017, 04:30:14 pm
Finally, the only one that worked for me was the idea with TabOrder.
Show an example of code where ActiveControl does not work.
Here is an example where TabOrder does not work:
Code: Pascal
[Select]
[+]
[-]
procedure
TForm1
.
FormCreate
(
Sender
:
TObject
)
;
function
NewEdit
(
X
,
Y
,
Width
,
Height
:
Integer
)
:
TEdit
;
begin
Result
:
=
TEdit
.
Create
(
Self
)
;
Result
.
Parent
:
=
Self
;
Result
.
SetBounds
(
X
,
Y
,
Width
,
Height
)
;
end
;
var
E1
,
E2
:
TEdit
;
begin
E1
:
=
NewEdit
(
8
,
8
,
100
,
24
)
;
E1
.
TabStop
:
=
False
;
E1
.
TabOrder
:
=
0
;
// Ignored
E2
:
=
NewEdit
(
7
,
40
,
100
,
24
)
;
E2
.
TabOrder
:
=
1
;
// ActiveControl := E1; // This work!
end
;
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
General
»
SetFocus fails
TinyPortal
© 2005-2018