Lazarus

Free Pascal => Beginners => Topic started by: Mountaineer on May 16, 2021, 07:38:02 pm

Title: change dimension and position of a form at runtime not working!?
Post by: Mountaineer on May 16, 2021, 07:38:02 pm
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!
Title: Re: change dimension and position of a form at runtime not working!?
Post by: engkin 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;
Title: Re: change dimension and position of a form at runtime not working!?
Post by: Bart 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
Title: Re: change dimension and position of a form at runtime not working!?
Post by: lucamar 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.
Title: Re: change dimension and position of a form at runtime not working!?
Post by: engkin on May 16, 2021, 08:08:03 pm
Yes, it is not needed to move to OnShow.
Title: Re: change dimension and position of a form at runtime not working!?
Post by: speter 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.
Title: Re: change dimension and position of a form at runtime not working!?
Post by: Mountaineer 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
Title: Re: change dimension and position of a form at runtime not working!?
Post by: Bart 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
Title: Re: change dimension and position of a form at runtime not working!?
Post by: wp 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.
Title: Re: change dimension and position of a form at runtime not working!?
Post by: Bart 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