Jump to content
Sign in to follow this  
RDP1974

tgridpanel resize

Recommended Posts

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
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. :classic_dry:

Share this post


Link to post

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.

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 by Der schöne Günther

Share this post


Link to post
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;

image.thumb.png.0c262611b4794351fde3bdd4279b7318.png

 

Lately, my mental health hasn't been good... so I'm using the computer more!

Edited by programmerdelphi2k

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×