Recent

Author Topic: [SOLVED] TLabel OptimalFill affecting Top-Positioning on Application Launch  (Read 1011 times)

JohannG

  • Newbie
  • Posts: 2
Hey there!

I've encountered an odd behaviour while trying to make a database application a bit more pretty: When using the OptimalFill property of a TLabel the Top property of said label isn't correctly assigned on application launch.

Code: Pascal  [Select][+][-]
  1. // Create label in MyForm.OnCreate event
  2.  
  3. procedure MyForm.FormCreate(Sender: TObject);
  4. begin
  5.   MyLabel := TLabel.Create(Self);
  6.   with MyLabel do begin
  7.     Name := 'MyLabel';
  8.     Parent := MyForm;
  9.     AutoSize := false;
  10.     OptimalFill := true;                        // This setting "activates" the strange behaviour
  11.     Layout := tlCenter;
  12.     Alignment := taCenter;
  13.     WordWrap := true;
  14.   end;    
  15. end;
  16.  
  17. // Set label dimensions in MyForm.OnResize event
  18.  
  19. procedure MyForm.FormResize(Sender: TObject);
  20. begin
  21.   ShowMessage(OtherComponent.Height.ToString()); // This shows a valid value.
  22.   MyLabel.Top := OtherComponent.Height + 8;      // Assigning a constant value doesn't change the behaviour.
  23.   ShowMessage(MyLabel.Top.ToString());           // This shows 0 when the application is launched and the correct value after every manual resize of the form.
  24.   MyLabel.Left := OtherComponent.Left;
  25.   MyLabel.Width := OtherComponent.Width;
  26.   MyLabel.Height := Round( ( MyForm.Height - 24 ) / 9 );
  27. end;
  28.  
  29. // Get label caption from SQLQuery.AfterScroll event
  30.  
  31. procedure MyForm.MySQLQueryAfterScroll();
  32. begin
  33.   MyLabel.Caption := MySQLQuery.FieldByName('LabelCaption').asString;
  34. end;
  35.  

I tried inserting all kinds of redraw operations and nothing really helped here. Maybe I'm missing something and this is the expected behavior... I would be very thankful for a few hints!

Kind regards
Johann
« Last Edit: October 10, 2022, 06:13:51 pm by JohannG »

jamie

  • Hero Member
  • *****
  • Posts: 7405
Re: TLabel OptimalFill affecting Top-Positioning on Application Launch
« Reply #1 on: October 09, 2022, 08:09:59 pm »
The form has not yet set its object pointer while inside the OnCreate event.

Use SELF for the object id instead of the external pointer of the form.

So the PARENT should be SELF not myform.

it maybe something else but I would start there.

also set the Parent outside the WIDTH block because I am not sure what SELF will return at that point?
The only true wisdom is knowing you know nothing

dseligo

  • Hero Member
  • *****
  • Posts: 1622
Re: TLabel OptimalFill affecting Top-Positioning on Application Launch
« Reply #2 on: October 09, 2022, 10:32:38 pm »
Please show declaration of your form and it's variable.

This is part of your code:
Code: Pascal  [Select][+][-]
  1. procedure MyForm.FormCreate(Sender: TObject);
  2. begin
  3.   MyLabel := TLabel.Create(Self);
  4.   with MyLabel do begin
  5.     Name := 'MyLabel';
  6.     Parent := MyForm;

If your form's type is MyForm, then it's variable isn't MyForm for sure.

Usually type has T in front of it, so it should more likely look like this:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyForm = class(TForm)
  3.    ...
  4.  
  5. var
  6.   MyForm: TMyForm;
  7.  
  8. ...
  9.  
  10. procedure TMyForm.FormCreate(Sender: TObject);
  11. begin
  12.   MyLabel := TLabel.Create(Self);
  13.   with MyLabel do begin
  14.     Name := 'MyLabel';
  15.     Parent := MyForm;

JohannG

  • Newbie
  • Posts: 2
Re: TLabel OptimalFill affecting Top-Positioning on Application Launch
« Reply #3 on: October 10, 2022, 06:13:06 pm »
The form has not yet set its object pointer while inside the OnCreate event.

Thanks a lot! I think this was the important part. I moved to label creation from the OnCreate to the OnShow event and now it behaves exactly as expected :)

 

TinyPortal © 2005-2018