Recent

Author Topic: [SOLVED]How to Automatically Resize Form to Fit Entire TStringGrid in Lazarus?  (Read 887 times)

Aruna

  • Hero Member
  • *****
  • Posts: 794
Hi, I have attached two screenshots for reference. The first shows what appears when I run the application. The second shows the desired appearance I want when the application starts. I have tried a few methods to make the form automatically resize, but none have worked so far. While I can manually drag and resize the form to get the layout I want, I don’t want to do this every time I run the app. Is there a way to make the form automatically resize so that it fits the entire TStringGrid on startup? The source is below.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids;
  9.  
  10. type
  11.   { TForm1 }
  12.  
  13.   TForm1 = class(TForm)
  14.     StringGrid1: TStringGrid;
  15.     procedure FormCreate(Sender: TObject);
  16.     procedure FormShow(Sender: TObject);
  17.   private
  18.     procedure PopulateDrugData;
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TForm1 }
  29.  
  30. procedure TForm1.PopulateDrugData;
  31. const
  32.   DrugData: array[0..11, 0..4] of string = (
  33.     ('Cannabis', 'Weed, Pot, Ganja, Bud, 420', 'Dried flowers, edibles', 'Relaxation, hunger, euphoria', 'Can impair memory/reaction; low overdose risk'),
  34.     ('Cocaine', 'Coke, Blow, Snow, Yayo', 'White powder', 'Energy, confidence, alertness', 'Heart stress, addiction, often laced'),
  35.     ('Crack Cocaine', 'Crack, Rocks', 'Hard crystals', 'Intense short high', 'Highly addictive, increased overdose risk'),
  36.     ('MDMA', 'Molly, Ecstasy, E, X', 'Pills, powder, crystals', 'Empathy, energy, sensory enhancement', 'Dehydration, serotonin syndrome'),
  37.     ('Methamphetamine', 'Meth, Crystal, Tina, Ice, Crank', 'Crystals, powder', 'Intense focus, energy', 'Extreme addiction, psychosis'),
  38.     ('Heroin', 'Smack, H, Dope, Skag', 'Brown/white powder, tar', 'Euphoria, sedation', 'Overdose, high dependency'),
  39.     ('Fentanyl', 'Fent, China Girl, Apache, Dance Fever', 'Powder, fake pills', 'Powerful sedation, pain relief', 'Microscopic dose = fatal; often contaminates others'),
  40.     ('GHB', 'Gina, G, Liquid X, Fantasy', 'Clear liquid, powder', 'Sedation, lowered inhibitions', 'Date rape drug; memory loss; coma risk'),
  41.     ('Ketamine', 'K, Special K, Kit Kat', 'Liquid, powder', 'Disconnection, hallucinations', 'Bladder damage, confusion'),
  42.     ('LSD', 'Acid, Tabs, Blotters', 'Blotter paper, drops', 'Visual/auditory hallucinations', 'Bad trips, anxiety'),
  43.     ('Psilocybin', 'Shrooms, Magic Mushrooms, Boomers', 'Dried mushrooms', 'Hallucinations, introspection', 'Nausea, confusion'),
  44.     ('Benzodiazepines', 'Xanax, Bars, Benzos', 'Pills', 'Sedation, anxiety reduction', 'Dependency, dangerous with alcohol')
  45.   );
  46. const
  47.   Headers: array[0..4] of string = (
  48.     'Drug Name', 'Street Names', 'Form', 'Common Effects', 'Risks / Notes'
  49.   );
  50. var
  51.   i, j: Integer;
  52. begin
  53.   StringGrid1.ColCount := 5;
  54.   StringGrid1.RowCount := Length(DrugData) + 1;
  55.  
  56.   // Set headers
  57.   for j := 0 to 4 do
  58.     StringGrid1.Cells[j, 0] := Headers[j];
  59.  
  60.   // Fill data
  61.   for i := 0 to High(DrugData) do
  62.     for j := 0 to 4 do
  63.       StringGrid1.Cells[j, i + 1] := DrugData[i, j];
  64.  
  65.   // Autosize columns
  66.   for j := 0 to 4 do
  67.     StringGrid1.ColWidths[j] := 120;
  68. end;
  69.  
  70. procedure TForm1.FormCreate(Sender: TObject);
  71. begin
  72.   PopulateDrugData;
  73.   StringGrid1.Options := StringGrid1.Options + [goColSizing, goRowSelect];
  74.   StringGrid1.AutoSizeColumns;
  75. end;
  76.  
  77. procedure TForm1.FormShow(Sender: TObject);
  78. begin
  79.     // Resize the form to fit the grid exactly
  80.   Width := StringGrid1.Left + StringGrid1.Width + 16;  // +16 for padding or scrollbar
  81.   Height := StringGrid1.Top + StringGrid1.Height + 39; // +39 accounts for titlebar, borders
  82. //  writeln('Width:'+inttostr(Width),' Height:'+inttostr(Height));
  83. //  writeln(StringGrid1.Width);
  84. end;
  85.  
  86. end.
  87.  
« Last Edit: June 26, 2025, 03:45:02 am by Aruna »

dsiders

  • Hero Member
  • *****
  • Posts: 1556
Hi, I have attached two screenshots for reference. The first shows what appears when I run the application. The second shows the desired appearance I want when the application starts. I have tried a few methods to make the form automatically resize, but none have worked so far. While I can manually drag and resize the form to get the layout I want, I don’t want to do this every time I run the app. Is there a way to make the form automatically resize so that it fits the entire TStringGrid on startup? The source is below.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids;
  9.  
  10. type
  11.   { TForm1 }
  12.  
  13.   TForm1 = class(TForm)
  14.     StringGrid1: TStringGrid;
  15.     procedure FormCreate(Sender: TObject);
  16.     procedure FormShow(Sender: TObject);
  17.   private
  18.     procedure PopulateDrugData;
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TForm1 }
  29.  
  30. procedure TForm1.PopulateDrugData;
  31. const
  32.   DrugData: array[0..11, 0..4] of string = (
  33.     ('Cannabis', 'Weed, Pot, Ganja, Bud, 420', 'Dried flowers, edibles', 'Relaxation, hunger, euphoria', 'Can impair memory/reaction; low overdose risk'),
  34.     ('Cocaine', 'Coke, Blow, Snow, Yayo', 'White powder', 'Energy, confidence, alertness', 'Heart stress, addiction, often laced'),
  35.     ('Crack Cocaine', 'Crack, Rocks', 'Hard crystals', 'Intense short high', 'Highly addictive, increased overdose risk'),
  36.     ('MDMA', 'Molly, Ecstasy, E, X', 'Pills, powder, crystals', 'Empathy, energy, sensory enhancement', 'Dehydration, serotonin syndrome'),
  37.     ('Methamphetamine', 'Meth, Crystal, Tina, Ice, Crank', 'Crystals, powder', 'Intense focus, energy', 'Extreme addiction, psychosis'),
  38.     ('Heroin', 'Smack, H, Dope, Skag', 'Brown/white powder, tar', 'Euphoria, sedation', 'Overdose, high dependency'),
  39.     ('Fentanyl', 'Fent, China Girl, Apache, Dance Fever', 'Powder, fake pills', 'Powerful sedation, pain relief', 'Microscopic dose = fatal; often contaminates others'),
  40.     ('GHB', 'Gina, G, Liquid X, Fantasy', 'Clear liquid, powder', 'Sedation, lowered inhibitions', 'Date rape drug; memory loss; coma risk'),
  41.     ('Ketamine', 'K, Special K, Kit Kat', 'Liquid, powder', 'Disconnection, hallucinations', 'Bladder damage, confusion'),
  42.     ('LSD', 'Acid, Tabs, Blotters', 'Blotter paper, drops', 'Visual/auditory hallucinations', 'Bad trips, anxiety'),
  43.     ('Psilocybin', 'Shrooms, Magic Mushrooms, Boomers', 'Dried mushrooms', 'Hallucinations, introspection', 'Nausea, confusion'),
  44.     ('Benzodiazepines', 'Xanax, Bars, Benzos', 'Pills', 'Sedation, anxiety reduction', 'Dependency, dangerous with alcohol')
  45.   );
  46. const
  47.   Headers: array[0..4] of string = (
  48.     'Drug Name', 'Street Names', 'Form', 'Common Effects', 'Risks / Notes'
  49.   );
  50. var
  51.   i, j: Integer;
  52. begin
  53.   StringGrid1.ColCount := 5;
  54.   StringGrid1.RowCount := Length(DrugData) + 1;
  55.  
  56.   // Set headers
  57.   for j := 0 to 4 do
  58.     StringGrid1.Cells[j, 0] := Headers[j];
  59.  
  60.   // Fill data
  61.   for i := 0 to High(DrugData) do
  62.     for j := 0 to 4 do
  63.       StringGrid1.Cells[j, i + 1] := DrugData[i, j];
  64.  
  65.   // Autosize columns
  66.   for j := 0 to 4 do
  67.     StringGrid1.ColWidths[j] := 120;
  68. end;
  69.  
  70. procedure TForm1.FormCreate(Sender: TObject);
  71. begin
  72.   PopulateDrugData;
  73.   StringGrid1.Options := StringGrid1.Options + [goColSizing, goRowSelect];
  74.   StringGrid1.AutoSizeColumns;
  75. end;
  76.  
  77. procedure TForm1.FormShow(Sender: TObject);
  78. begin
  79.     // Resize the form to fit the grid exactly
  80.   Width := StringGrid1.Left + StringGrid1.Width + 16;  // +16 for padding or scrollbar
  81.   Height := StringGrid1.Top + StringGrid1.Height + 39; // +39 accounts for titlebar, borders
  82. //  writeln('Width:'+inttostr(Width),' Height:'+inttostr(Height));
  83. //  writeln(StringGrid1.Width);
  84. end;
  85.  
  86. end.
  87.  

As you've discovered, TStringGrid.Width is not what you need. The GCache.GridWidth property is the value you;'re looking for. You could also accumlate the width manually for each column... add width for grid lines... and add scroll bar width. But that's essentially what TStringGrid.GCache.GridWidth contains.

Aruna

  • Hero Member
  • *****
  • Posts: 794
As you've discovered, TStringGrid.Width is not what you need. The GCache.GridWidth property is the value you;'re looking for. You could also accumlate the width manually for each column... add width for grid lines... and add scroll bar width. But that's essentially what TStringGrid.GCache.GridWidth contains.
Many thanks @dsiders I went with your 'accumulate the column width ' and it works great. What I did is below:
Code: Pascal  [Select][+][-]
  1. function GetGridContentWidth(Grid: TStringGrid): Integer;
  2. var
  3.   i: Integer;
  4. begin
  5.   Result := 0;
  6.   for i := 0 to Grid.ColCount - 1 do
  7.     Result := Result + Grid.ColWidths[i];
  8.  
  9.   // Add margin for borders or padding
  10.   Result := Result + 2;
  11. end;                              

 

TinyPortal © 2005-2018