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
Xcode Command Line Tools ...
by
Joseph
[
Today
at 04:58:03 pm]
Offical launch of the 1 B...
by
Hartmut
[
Today
at 04:49:29 pm]
Parameter passing odditie...
by
alpine
[
Today
at 04:47:42 pm]
what to do if my target M...
by
nickysn
[
Today
at 04:16:02 pm]
trunk compiler bug for mi...
by
nickysn
[
Today
at 04:03:44 pm]
compiler error in unit
by
paule32
[
Today
at 03:49:50 pm]
Compile/Convert Delphi pr...
by
Joseph
[
Today
at 02:43:45 pm]
how to tweek fpmake to co...
by
Key-Real
[
Today
at 02:33:27 pm]
Can function be used for ...
by
VisualLab
[
Today
at 02:15:30 pm]
Multithreading - synchron...
by
mika
[
Today
at 01:48:51 pm]
A fairly simple sound sol...
by
KodeZwerg
[
Today
at 01:48:26 pm]
May be useful to somebody
by
KodeZwerg
[
Today
at 12:38:22 pm]
match automatic highlight...
by
Чебурашка
[
Today
at 11:35:14 am]
Tchart with dual numbers ...
by
wp
[
Today
at 10:48:18 am]
Generics - correct syntax
by
VisualLab
[
Today
at 10:23:16 am]
add new targets to make
by
Key-Real
[
Today
at 09:38:23 am]
Debugger crashes with App...
by
Martin_fr
[
Today
at 09:29:46 am]
Copyrights Q
by
BrassGear
[
Today
at 09:13:14 am]
[SOLVED] how to build the...
by
Key-Real
[
Today
at 09:12:33 am]
Quirky windows
by
tetrastes
[
Today
at 08:59:06 am]
How can I draw a rectangl...
by
Handoko
[
Today
at 07:30:55 am]
Draw a line in the Scroll...
by
Handoko
[
Today
at 07:27:54 am]
web development using pas...
by
Nate897
[
Today
at 02:04:05 am]
questionable function def...
by
440bx
[
Today
at 01:35:28 am]
Why isn't TTIRadioGroup's...
by
jamie
[
Today
at 12:13:45 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: SetFocus fails (Read 6074 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: 14377
Sensorship about opinions does not belong here.
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
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.
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: 2246
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: 2246
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