* * *

Author Topic: SetFocus fails  (Read 2620 times)

dietmar

  • Jr. Member
  • **
  • Posts: 76
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
Lazarus 1.6.4 with FPC 3.0.2 on Windows10 64Bit

Thaddy

  • Hero Member
  • *****
  • Posts: 6151
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...
I might not give the answer that you want me to.. Peter Green 1969

dietmar

  • Jr. Member
  • **
  • Posts: 76
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.
Lazarus 1.6.4 with FPC 3.0.2 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.
Lazarus 1.8.0 FPC 3.0.2 SVN 56594 Windows 10 64bit (i386-win32-win32/win64)

RAW

  • Hero Member
  • *****
  • Posts: 664
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).
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.2 FPC 3.0.4 // 1.7 FPC 3.1.1

dietmar

  • Jr. Member
  • **
  • Posts: 76
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
Lazarus 1.6.4 with FPC 3.0.2 on Windows10 64Bit

RAW

  • Hero Member
  • *****
  • Posts: 664
Re: SetFocus fails
« Reply #6 on: September 12, 2017, 12:10:39 am »
Try this...

Code: Pascal  [Select]
  1. btn.TabOrder:= 0;
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.2 FPC 3.0.4 // 1.7 FPC 3.1.1

ASerge

  • Hero Member
  • *****
  • Posts: 780
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.

RAW

  • Hero Member
  • *****
  • Posts: 664
Re: SetFocus fails
« Reply #8 on: September 12, 2017, 02:37:11 am »
Really ??? How do you use this @ASerge... ?

Code: Pascal  [Select]
  1. Procedure TForm1.Button1Click(Sender: TObject);
  2.   Var
  3.    labInfo: TLabel;
  4.    memo   : TMemo;
  5.    btn    : TButton;
  6.    wndNew : TForm;
  7.  Begin
  8.   wndNew:= TForm.Create(Self);
  9.    Try
  10.     wndNew.Caption              := 'MyNewWindow';
  11.     wndNew.Constraints.MinHeight:= 350;
  12.     wndNew.Constraints.MinWidth := 400;
  13.     wndNew.DoubleBuffered       := True;
  14.     wndNew.SetBounds            ((Screen.WorkAreaWidth -700) Div 2,
  15.                                  (Screen.WorkAreaHeight-500) Div 2,
  16.                                   700, 500);
  17.  
  18.     memo             := TMemo.Create(Self);
  19.     memo.Align       := alTop;
  20.     memo.Height      := 200;
  21.     memo.Text        := 'Type in whatever you like...';
  22.     memo.WordWrap    := True;
  23.     memo.Font.Quality:= fqAntialiased;
  24.     memo.Parent      := wndNew;
  25.  
  26.     labInfo             := TLabel.Create(Self);
  27.     labInfo.Caption     := 'Hello Everybody...';
  28.     labInfo.Align       := alTop;
  29.     labInfo.Font.Size   := 15;
  30.     labInfo.Font.Quality:= fqAntialiased;
  31.     labInfo.AutoSize    := True;
  32.     labInfo.Parent      := wndNew;
  33.  
  34.     btn             := TButton.Create(Self);
  35.     btn.Caption     := 'I am a button...';
  36.     btn.Font.Quality:= fqAntialiased;
  37.     btn.Font.Size   := labInfo.Font.Size;
  38.     btn.Align       := alBottom;
  39.     btn.Parent      := wndNew;
  40.     btn.TabOrder    := 0;  //works nice !!!
  41.     btn.AutoSize    := True;
  42.  
  43.     //ActiveControl:= btn;   Error !!!
  44.  
  45.     wndNew.ShowModal;
  46.    Finally
  47.     wndNew.Release;
  48.     wndNew:= Nil;
  49.    End;
  50.  End;
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.2 FPC 3.0.4 // 1.7 FPC 3.1.1

PatBayford

  • Full Member
  • ***
  • Posts: 125
Re: SetFocus fails
« Reply #9 on: September 12, 2017, 04:57:44 am »
Code: Pascal  [Select]
  1.     btn.AutoSize    := True;
  2.  
  3.     //ActiveControl:= btn;   Error !!!
  4.  
  5.     wndNew.ShowModal;
Try using wndwNew.ActiveControl := btn;
Lazarus 1.8.0 FPC 3.0.2 SVN 56594 Windows 10 64bit (i386-win32-win32/win64)

Zaxxon

  • New member
  • *
  • Posts: 17
Re: SetFocus fails
« Reply #10 on: September 12, 2017, 08:17:11 am »
Try

Code: Pascal  [Select]
  1. if dlgCustom2.Button1.Visible then dlgCustom2.Button1.SetFocus;

RAW

  • Hero Member
  • *****
  • Posts: 664
Re: SetFocus fails
« Reply #11 on: September 12, 2017, 01:01:42 pm »
@PatBayford: Thanks, yes this is working fine:
Code: Pascal  [Select]
  1. wndNew.ActiveControl:= btn;


@Zaxxon: Visible and SetFocus doesn't work for me...

Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.2 FPC 3.0.4 // 1.7 FPC 3.1.1

dietmar

  • Jr. Member
  • **
  • Posts: 76
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
Lazarus 1.6.4 with FPC 3.0.2 on Windows10 64Bit

ASerge

  • Hero Member
  • *****
  • Posts: 780
Re: SetFocus fails
« Reply #13 on: September 15, 2017, 01:44:39 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]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2.  
  3.   function NewEdit(X, Y, Width, Height: Integer): TEdit;
  4.   begin
  5.     Result := TEdit.Create(Self);
  6.     Result.Parent := Self;
  7.     Result.SetBounds(X, Y, Width, Height);
  8.   end;
  9.  
  10. var
  11.   E1, E2: TEdit;
  12. begin
  13.   E1 := NewEdit(8, 8, 100, 24);
  14.   E1.TabStop := False;
  15.   E1.TabOrder := 0; // Ignored
  16.   E2 := NewEdit(7, 40, 100, 24);
  17.   E2.TabOrder := 1;
  18. //  ActiveControl := E1; // This work!
  19. end;

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus