Recent

Author Topic: [SOLVED] StringGrid Horizontal scrollbar always present  (Read 1622 times)

indydev

  • Full Member
  • ***
  • Posts: 127
[SOLVED] StringGrid Horizontal scrollbar always present
« on: January 22, 2024, 09:07:19 pm »
I am dynamically creating a StringGrid placing it into a TPanel and assigning it to a private Field (thus it is called FTable). The TPanel itself is dynamically created. The data for the StringGrid data is assigned from a streamed source (and handled elsewhere). The StringGrid displays fine, and correctly displays the data. However, no matter what I do, I can't get the Horizontal Scrollbar to disappear. ssNone does not work.  ssAutoHorizontal and ssAutoBoth, even if I resize the StringGrid still displays the horizontal scrollbar. The vertical scrollbar works correctly--or at least how I expect it to.

I have included the entire procedure code just in case, but much of it is probably irrelevant to the issue.

I am using Linux Mint 21.2.

Code: Pascal  [Select][+][-]
  1. procedure TDiscussionPanel.CreateTable(MLeft, MWidth: integer; DText: string);
  2. const
  3.   border = 8;
  4. var
  5.   tblData: TTableData;
  6.   i, j, TotalWidth: integer;
  7. begin
  8.   tblData := ParseMarkdownTable(DText);
  9.   FTable := TStringGrid.Create(Parent);
  10.   FTable.Parent := Self;
  11.   FTable.ScrollBars:=ssNone;
  12.   FTable.Font.Size := 12;
  13.   FTable.DefaultRowHeight := (FTable.Font.Size + 1) * 2;
  14.   Self.Height := FTable.DefaultRowHeight * tblData.RowCount;
  15.   FTable.FixedRows:=0;
  16.   FTable.RowCount := tblData.RowCount;
  17.   FTable.ColCount := tblData.ColCount;
  18.   FTable.Flat := TRUE;
  19.   FTable.ParentColor:=TRUE;
  20.   FTable.BorderColor := clBlueSilver;
  21.   FTable.Options:=[goFixedHorzLine, goFixedVertLine, goHorzLine, goRangeSelect, goVertLine];
  22.   FTable.FixedCols:=0;
  23.    if tblData.Header then begin
  24.      FTable.FixedRows:=1;
  25.      FTable.FixedGridLineColor := clBlueSilver;
  26.      FTable.OnPrepareCanvas := @TablePrepareCanvas;
  27.    end;
  28.   FTable.GridLineColor := clBlueSilver;
  29.   FTable.TitleFont.Color := clBlueSilver;
  30.   FTable.Font.Color := clBlueSilver;
  31.   TotalWidth := 0;
  32.   for J := 0 to tblData.RowCount - 1 do begin
  33.     for i := 0 to FTable.ColCount - 1 do begin
  34.       FTable.Cells[i, j] := tblData.ColRow[i, j];
  35.       if j = FTable.RowCount - 1 then begin
  36.        FTable.AutoSizeColumn(i);
  37.        TotalWidth := TotalWidth + FTable.ColWidths[i];
  38.       end;
  39.     end;
  40.   end;
  41.   FTable.Width := TotalWidth + 1;
  42.   FTable.Height:= (FTable.DefaultRowHeight + 1) * (FTable.RowCount + 2);
  43.   FTable.Anchors:=[akTop, akBottom];
  44.   FTable.AnchorSide[akTop].Control := FTable.Parent;
  45.   FTable.AnchorSide[akTop].Side := asrTop;
  46.   FTable.AnchorHorizontalCenterTo(Parent);
  47.   FTable.AnchorSideBottom.Control := FTable.Parent;
  48.   FTable.AnchorSideBottom.Side := asrBottom;
  49.   FTable.BorderSpacing.Left := border;
  50.   FTable.BorderSpacing.Right:= border;
  51.   FTable.BorderSpacing.Bottom:= border;
  52.   FTable.BorderSpacing.Top:=border;
  53.   FLeft := MLeft;
  54.   FWidth:= MWidth;
  55.   FTable.Left := FLeft + (FWidth - FTable.Width) div 2;
  56. end;
« Last Edit: January 23, 2024, 04:58:43 pm by indydev »

wp

  • Hero Member
  • *****
  • Posts: 13216
Re: StringGrid Horizontal scrollbar always present
« Reply #1 on: January 22, 2024, 11:30:24 pm »
Can you extract the issue into a small compilable project? Pack .pas, .lfm, .lpr and .lpi files into a common .zip and upload that under "Attachments and other options" (do not include any compiler-generated files, please).

madref

  • Hero Member
  • *****
  • Posts: 1111
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: StringGrid Horizontal scrollbar always present
« Reply #2 on: January 23, 2024, 01:00:51 am »
A TStringGrid has the options of Scrollbars (See attachment).
You can uses this to set or not to set the scollbyrs


Or add to your TotalWidth 10 instead of 1 and run it and see what happens. If the horizontal scrollbar is still there increase it to get the right with. If it's gone do the same but decrease is.


It's sometimes a bit of trial and error  :D
« Last Edit: January 23, 2024, 01:03:33 am by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sequoaia 15.6.1
Lazarus 4.99 (rev main_4_99-2644-gfd63613782) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

indydev

  • Full Member
  • ***
  • Posts: 127
Re: StringGrid Horizontal scrollbar always present
« Reply #3 on: January 23, 2024, 03:38:43 am »
I have attached a zip with a simple form and a button to dynamically create a TPanel and a TStringGrid.

@madref  The scrollbars property is not working correctly in this case. ssNone, ssAutoHorizontal, ssAutoBoth all leave the Horizontal scrollbar no matter the width. ssAutoVertical works however.

wp

  • Hero Member
  • *****
  • Posts: 13216
Re: StringGrid Horizontal scrollbar always present
« Reply #4 on: January 23, 2024, 11:16:58 am »
Could not reproduce the issue on Windows, but on Linux Mint I could.

Playing with the issue for a while I found out, that the scrollbar goes away when the Scrollbars property is set before setting the Parent:
Code: Pascal  [Select][+][-]
  1.    NewPanel.FTable := TStringGrid.Create(Parent);
  2.    NewPanel.FTable.ScrollBars:=ssNone;    // Must be before setting the Parent
  3.    NewPanel.FTable.Parent := NewPanel;  
  4.    ...

indydev

  • Full Member
  • ***
  • Posts: 127
Re: StringGrid Horizontal scrollbar always present
« Reply #5 on: January 23, 2024, 04:13:22 pm »
I can confirm your solution on Linux Mint. I moved the scrollbars setting around many places, but didn't try that. Thank you! I don't have a Windows machine here, and eventually I will have to test on Windows. Do you know if this messes anything up on Windows?

wp

  • Hero Member
  • *****
  • Posts: 13216
Re: StringGrid Horizontal scrollbar always present
« Reply #6 on: January 23, 2024, 04:34:58 pm »
I can confirm your solution on Linux Mint. I moved the scrollbars setting around many places, but didn't try that. Thank you! I don't have a Windows machine here, and eventually I will have to test on Windows. Do you know if this messes anything up on Windows?
No, your project was fine on Windows (with setting scrollbars before or after setting the Parent)

indydev

  • Full Member
  • ***
  • Posts: 127
Re: [SOLVED] StringGrid Horizontal scrollbar always present
« Reply #7 on: January 23, 2024, 04:59:11 pm »
Great!

 

TinyPortal © 2005-2018