Recent

Author Topic: Bug with TstringGrid in a frame  (Read 1170 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 380
Bug with TstringGrid in a frame
« on: August 12, 2025, 11:24:00 pm »
Created a frame.
Dropped a TStringGrid on it, set ColCount to 1 and FixedCol to 1
Created two columns.
In the frame's constructor I've got:
Code: Pascal  [Select][+][-]
  1. constructor TFrame1.Create(TheOwner: TComponent);
  2. begin
  3.   inherited Create(TheOwner);
  4.   StringGrid1.ColWidths[0] := 25;
  5. end;
  6.  

Dropped the grid on the project's main form and ran the app. The fixed column's width was adjusted the first time I ran the project. After that, the adjustment stopped working.

I've added a private attribute fSG of type TFrame1 to the main form, and in the OnCreate I added this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   fsg := TFrame1.create(Self);
  4.   fsg.Parent := self;
  5.   fsg.Align  := alClient;
  6. end;
  7.  

The width of the fixed col for fSG works correctly. The one for the dropped frame doesn't.

Attached project.

Could someone confirm this behavior? Also, should I report it as a bug?

wp

  • Hero Member
  • *****
  • Posts: 13210
Re: Bug with TstringGrid in a frame
« Reply #1 on: August 13, 2025, 01:16:27 am »
I can confirm it. But I would not recommend to add a frame to a form at designtime - there are many more bad surprises than what you are seeing. It is much safer to add frames as runtime, just like you are doing with the other frame.

EganSolo

  • Sr. Member
  • ****
  • Posts: 380
Re: Bug with TstringGrid in a frame
« Reply #2 on: August 13, 2025, 01:41:01 am »
Thanks, wp.
Is it worth reporting this as a bug, or should I chug it to adding a frame to a form at design time?

CharlyTango

  • Full Member
  • ***
  • Posts: 168
Re: Bug with TstringGrid in a frame
« Reply #3 on: August 13, 2025, 07:50:20 am »
The problems with frames at design time have been well known since the first versions of Delphi.
No reason for a bug report.

Frames are a good tool; you can fill them with controls at design time, but you should never use them anywhere at design time.

As long as you create them at runtime and integrate them using the parent property, they work very well.
I usually use a kind of placeholder for this, such as a TPanel.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   fsg: TFrameXY;
  4. begin
  5.   fsg := TFrameXY.create(Self);
  6.   fsg.Parent := PanelForFrame1;
  7.   fsg.Align  := alClient;
  8. end;
Lazarus stable, Win32/64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11800
  • Debugger - SynEdit - and more
    • wiki
Re: Bug with TstringGrid in a frame
« Reply #4 on: August 13, 2025, 08:31:04 am »
I think this is the correct behaviour.

The frame and with it the column width are loaded from the lfm.
There is a difference in order between a top-level form (or frame) being loaded, and any component (including frame) on it.

The top level frame/form calls the loadin in its constructor. So once you done "inherited" in the TForm.Create, then its already fully loaded (except for DPI scaling, and maybe a few things)

But if a component on the form (including a frame) is loaded from lfm, then the lfm loader creates it => and the loader can only set its properties once create has been fully executed (and therefore your change values will be overwritten).



The solution is
Code: Pascal  [Select][+][-]
  1. procedure Loaded; override;

jamie

  • Hero Member
  • *****
  • Posts: 7306
Re: Bug with TstringGrid in a frame
« Reply #5 on: August 14, 2025, 02:11:15 am »
isn't there a "OnAfterConstruction" in the TFORM ?

Wouldn't that be the place to such things?

Oh Well, I guess that gets called early before the OnCreate.

Just ignore me.

Jamie
« Last Edit: August 14, 2025, 02:38:08 am by jamie »
The only true wisdom is knowing you know nothing

cdbc

  • Hero Member
  • *****
  • Posts: 2464
    • http://www.cdbc.dk
Re: Bug with TstringGrid in a frame
« Reply #6 on: August 14, 2025, 09:51:17 am »
Hi
@jamie: Close mate  :D I think what you're referring to is "TObject.AfterConstruction; virtual;" ...and IIRC it's called just before the 'OnCreate' event is fired... You can override it in every class, as well as "BeforeDestruction" -- I tend to use those 2 rather than the 'OnCreate/OnDestroy' events  8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

jamie

  • Hero Member
  • *****
  • Posts: 7306
Re: Bug with TstringGrid in a frame
« Reply #7 on: August 14, 2025, 11:36:44 am »
I guess you could still override the before Construction procedure call the inherited and then execute your code afterwards should be the same
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018