Recent

Author Topic: change dimension and position of a form at runtime not working!?  (Read 3048 times)

Mountaineer

  • New Member
  • *
  • Posts: 11
Hi folks!

for most of you I'm sure this is a trivial problem, but I've been looking for a solution for hours (also here in the forum, of course), but haven't found anything suitable yet!
I try to resize a form during runtime but changing width and height only works in designer...!?
Tried to resize the form directly after creating it, but nothing happens...

The code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.  
  4.   Form1.Left:=10;
  5.   Form1.Top:=10;
  6.   Form1.Width:=1900;
  7.   Form1.Height:=1060;
  8.  
  9. end;
  10.  

Please help!

engkin

  • Hero Member
  • *****
  • Posts: 2857
Re: change dimension and position of a form at runtime not working!?
« Reply #1 on: May 16, 2021, 07:52:19 pm »
Use OnShow instead:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormShow(Sender: TObject);
  2. begin
  3.   Left:=10;
  4.   Top:=10;
  5.   Width:=1900;
  6.   Height:=1060;
  7. end;

Bart

  • Hero Member
  • *****
  • Posts: 4345
    • Bart en Mariska's Webstek
Re: change dimension and position of a form at runtime not working!?
« Reply #2 on: May 16, 2021, 07:57:46 pm »
That should not make a difference.

Are you sure that in the ObjectInspector the Form1's OnCreate is set to TForm1.FormCreate?
You may have accidentally deselected that.
Or maybe you just wrote the TForm1.FormCreate by hand and did not set it in ObjectInspector?

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 4150
Re: change dimension and position of a form at runtime not working!?
« Reply #3 on: May 16, 2021, 08:02:55 pm »
That should not make a difference.

Indeed! It works as it should here.

Just one hint: instead of accessing the variable Form1 you should use the (implicit) Self, as in
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Left:=10;
  4.   Top:=10;
  5.   Width:=1900;
  6.   Height:=1060;
  7. end;

It makes little or no difference if there is only that one instance of the class but it's better practice.

ETA
Oh! Another thing: if the form's Position is any other than poDesigned that might be interfering with the repositioning, the resizing or both. In that case, deferring them until OnActivate should solve the problem.
« Last Edit: May 16, 2021, 08:10:20 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

engkin

  • Hero Member
  • *****
  • Posts: 2857
Re: change dimension and position of a form at runtime not working!?
« Reply #4 on: May 16, 2021, 08:08:03 pm »
Yes, it is not needed to move to OnShow.

speter

  • Full Member
  • ***
  • Posts: 216
Re: change dimension and position of a form at runtime not working!?
« Reply #5 on: May 17, 2021, 01:49:19 am »
Also, have a look at the form's position property. I have found that it can interfere with resizing the form if it is NOT set to the default ("designed").

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

Mountaineer

  • New Member
  • *
  • Posts: 11
Re: change dimension and position of a form at runtime not working!?
« Reply #6 on: May 18, 2021, 07:49:11 am »
Many thanks to all of you!  :)

Finally it WORKS!!!

I tried the following:
Compared the properties of the non working project-form with a reference-form of a standard project...
...and I noticed that some properties of the forms differ.
Conclusion: "AutoSize" of the form MUST be false!  "Scaled" of the form MUST be true!

 :D

Mountaineer

Bart

  • Hero Member
  • *****
  • Posts: 4345
    • Bart en Mariska's Webstek
Re: change dimension and position of a form at runtime not working!?
« Reply #7 on: May 18, 2021, 02:19:43 pm »
Conclusion: ... "Scaled" of the form MUST be true!

If that is the case, then that really is a bug.

Bart

wp

  • Hero Member
  • *****
  • Posts: 8567
Re: change dimension and position of a form at runtime not working!?
« Reply #8 on: May 18, 2021, 02:35:49 pm »
"Scaled" of the form MUST be true!
This certainly is wrong. The attached projects has Scale=false and still its position and size can be changed in the OnCreate event of the form. Opposite to AutoSize=true, in which the commands to set width and height are overridden by the actual size of the form - that's what AutoSize is good for. But still Left and Top can be changed even with AutoSize=true.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Bart

  • Hero Member
  • *****
  • Posts: 4345
    • Bart en Mariska's Webstek
Re: change dimension and position of a form at runtime not working!?
« Reply #9 on: May 18, 2021, 06:19:29 pm »
I can confirm the findings of wp.

Conclusion so far: user error, not a bug.

Bart

 

TinyPortal © 2005-2018