RDP1974 40 Posted December 14, 2022 hi I have a TGridPanel with 3 columns set as ssPercent 33.3% and the proportions are fine now I set at runtime the column 3 at 0% then the screen redraws ok with the first 2 columns at 50% then I set back the column 3 at 33.3% but the proportions are not ok, the resulting column is smaller any hint? thanks Share this post Link to post
PeterBelow 238 Posted December 14, 2022 1 minute ago, RDP1974 said: hi I have a TGridPanel with 3 columns set as ssPercent 33.3% and the proportions are fine now I set at runtime the column 3 at 0% then the screen redraws ok with the first 2 columns at 50% then I set back the column 3 at 33.3% but the proportions are not ok, the resulting column is smaller any hint? thanks That is an old problem you also have at design-time: using ssPercent each change of a column width recalculates all column widths, including the one you just changed, so you end up with a value different from what you just entered. The algorithm used is faulty, but at design-time it is fairly easy to work around by changing the width in the DFM file directly. If you need to change the width at run-time it is easier to work with absolute column widths and do the calculations yourself, at least it is better for your mental health. Share this post Link to post
Der schöne Günther 316 Posted December 14, 2022 (edited) The algorithm is not faulty, the problem is that they always try to get to 100%. At runtime, you can easily set them all at once. Do this by calling BeginUpdate() and EndUpdate() on the column collection. 2022-12-14-12-32-02.mp4 Also, by setting all columns to zero, they will take up their space equally. Here is the checkbox from the video above: procedure TForm2.CheckBox1Click(Sender: TObject); begin const cols = GridPanel1.ColumnCollection; if(Sender as TCheckBox).Checked then begin cols.BeginUpdate(); try for var index := 0 to Pred(cols.Count) do cols[index].Value := 0.0; finally cols.EndUpdate(); end; end else cols[0].Value := 0.0; end; Edited December 14, 2022 by Der schöne Günther Share this post Link to post
programmerdelphi2k 237 Posted December 14, 2022 (edited) procedure TForm1.CreateMyGridPanelColumns; var LColumnItem: TColumnItem; LRowItem : TRowItem; LButton : TButton; begin GridPanel1.ColumnCollection.Clear; GridPanel1.RowCollection.Clear; // GridPanel1.BeginUpdate; // avoid "premature" repaints try LRowItem := GridPanel1.RowCollection.Add; LRowItem.SizeStyle := TSizeStyle.ssAbsolute; LRowItem.Value := 100; // LColumnItem := GridPanel1.ColumnCollection.Add; LColumnItem.SizeStyle := TSizeStyle.ssPercent; LColumnItem.Value := 33.33; // LColumnItem := GridPanel1.ColumnCollection.Add; LColumnItem.SizeStyle := TSizeStyle.ssPercent; LColumnItem.Value := 33.33; // LColumnItem := GridPanel1.ColumnCollection.Add; LColumnItem.SizeStyle := TSizeStyle.ssPercent; LColumnItem.Value := 33.33; finally GridPanel1.EndUpdate; end; // for var i: Integer := 1 to 3 do begin LButton := TButton.Create(Self); LButton.Parent := GridPanel1; LButton.Align := TAlign.alClient; LButton.Caption := LButton.Width.ToString; end; end; procedure TForm1.FormResize(Sender: TObject); begin for var i: Integer := 0 to (GridPanel1.ControlCount - 1) do if (GridPanel1.Controls[i] is TButton) then TButton(GridPanel1.Controls[i]).Caption := TButton(GridPanel1.Controls[i]).Width.ToString; end; procedure TForm1.GridPanel1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if (ssCtrl in Shift) and (Button = TMouseButton.mbLeft) then CreateMyGridPanelColumns; end; Lately, my mental health hasn't been good... so I'm using the computer more! Edited December 14, 2022 by programmerdelphi2k Share this post Link to post