Recent

Author Topic: help aligning components  (Read 1694 times)

glubbish

  • New Member
  • *
  • Posts: 46
help aligning components
« on: August 02, 2020, 11:53:40 pm »
I am trying to align 5 columns of TEdit beside the components of a TCheckgroup.
The code works but is inelegant, having to use offsets for top and left.
I tried to align them but could not get that to work (lack the skills).
if someone can show me how to rewrite this better I would appreciate it.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.CreateStatObjects;
  2. var
  3.   i, j: integer;
  4. begin
  5.   for i := 0 to 4 do
  6.     for j := 0 to CheckSelectDir.Items.Count - 1 do
  7.     begin
  8.       gStats[i, j]           := TEdit.Create(Form1);
  9.       gStats[i, j].Font      := CheckSelectDir.Font;
  10.       gStats[i, j].AutoSize  := False;
  11.       gStats[i, j].Alignment := taRightJustify;
  12.       gStats[i, j].Width     := gettextwidth('Updated', gStats[i, j].Font);
  13.       gStats[i, j].Height    := CheckSelectDir.Controls[j].Height;
  14.       gStats[i, j].Top       := CheckSelectDir.Top + CheckSelectDir.Controls[j].Top + 22;
  15.       gStats[i, j].Visible   := True;
  16.       gStats[i, j].ReadOnly  := True;
  17.       gStats[i, j].Alignment := taRightJustify;
  18.       gStats[i, j].Left      := CheckSelectDir.Left + CheckSelectDir.Width + 5 +  i * gStats[i, j].Width;
  19.       gStats[i, j].parent    := form1;
  20.     end;
  21. end;  

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: help aligning components
« Reply #1 on: August 03, 2020, 12:04:55 am »
maybe you are taking the wrong approach to the task ?

 Could you use a TStringGrid instead and do the drawing of the grid yourself where by you can then have control over the state of each cell.

 TstringGrid has a Editor in it so you can have that feature that you need.

 Would be possible to show a little image of what you have now so we could get an idea of what you are after ?

The only true wisdom is knowing you know nothing

glubbish

  • New Member
  • *
  • Posts: 46
Re: help aligning components
« Reply #2 on: August 03, 2020, 12:08:32 am »
Here is a screen shot.

wp

  • Hero Member
  • *****
  • Posts: 11906
Re: help aligning components
« Reply #3 on: August 03, 2020, 12:46:50 am »
The easiest way to automatically align controls within a container is to use the ChildSizing properties of the container. Since your form is occupied by other controls as well you should add a panel for the edit controls. Then add this code which adds the edits at runtime and also sets the ChildSizing of the panel (the latter can be done also in the object inspector):

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. const
  3.   N = 15;
  4. var
  5.   i: Integer;
  6. begin
  7.   Panel1.ChildSizing.ControlsPerLine := 3;
  8.   Panel1.ChildSizing.Layout := cclLeftToRightThenTopToBottom;
  9.   Panel1.ChildSizing.HorizontalSpacing := 3;
  10.   Panel1.ChildSizing.VerticalSpacing := 3;
  11.   // If you want the edits to grow/shrink with their container
  12.   Panel1.ChildSizing.EnlargeHorizontal := crsScaleChilds;
  13.   Panel1.ChildSizing.ShrinkHorizontal := crsScaleChilds;
  14.   SetLength(FEdit, N);
  15.   for i := 0 to N-1 do begin
  16.     FEdit[i] := TEdit.Create(self);
  17.     FEdit[i].Parent := Panel1;
  18.     FEdit[i].Alignment := taRightJustify;
  19.     FEdit[i].ReadOnly := true;
  20.   end;
  21.   Panel1.AutoSize := true;
  22. end;

glubbish

  • New Member
  • *
  • Posts: 46
Re: help aligning components
« Reply #4 on: August 03, 2020, 01:26:30 am »
Could not get this to work.
Tried putting the checkgroup on the panel, results were worse.
The edits did not line up with the checkgroup controls.

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: help aligning components
« Reply #5 on: August 03, 2020, 02:06:09 am »
You can use a TDrawGrid or String grid if you like to do what you are attempting..

It would be a single control when completed but it has headers , check marks, buttons and EDIT fields etc..

  You can color them and so on..

 The issue I have found is there seems to be a bug in the alignment for the checkmarks , They work at design time but are centered always a runtime unless  you reset their positions at runtime which can be done using the OnGetCheckState event..

 There is a property "Columns" which allows  you to get free with the cells and make columns behave like Buttons, Checks or just edit fields etc..

 With that you shouldn't have any alignment issues because they are all gridded together..

The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 2496
Re: help aligning components
« Reply #6 on: August 03, 2020, 02:30:09 am »
I tend to agree with jamie there, if only for the fact that when you run things on another machine/platform that isn't using the exact same config/setup (fonts, dpi etc) then your are bound to run into other alignment related issues as well.

First thing I have had to learn is to never attempt aligning individual items of a checkgroup with anything, as it will fail for sure. It would even be better to use individual check-items for such cases (but even then things can fail with regards to alignment). Those checkboxes can behave pretty weird on some platforms.

glubbish

  • New Member
  • *
  • Posts: 46
Re: help aligning components
« Reply #7 on: August 06, 2020, 12:36:53 am »
Tried with individual checkboxes (well an array of checkboxes).
Procedure is now:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.CreateStatObjects(BorderAllowance: integer);
  2. var
  3.   i, j: integer;
  4. begin
  5.   for i := 0 to 4 do
  6.     for j := 0 to DirCount do
  7.     begin
  8.       gStats[i, j]           := TEdit.Create(Form1);
  9.       gStats[i, j].Font      := CheckDirs[j].Font;
  10.       gStats[i, j].AutoSize  := False;
  11.       gStats[i, j].Alignment := taRightJustify;
  12.       gStats[i, j].Width     := gettextwidth('Updated', gStats[i, j].Font);
  13.       gStats[i, j].Height    := CheckDirs[j].Height;
  14.       gStats[i, j].Top       := CheckDirs[j].Top;
  15.       gStats[i, j].Visible   := True;
  16.       gStats[i, j].ReadOnly  := True;
  17.       gStats[i, j].Alignment := taRightJustify;
  18.       gStats[i, j].Left      := CheckDirs[j].Left + CheckDirs[MaxDirWidth].Width + BorderAllowance + i * gStats[i, j].Width;
  19.       gStats[i, j].parent    := form1;
  20.     end;
  21. end;  

Thanks for the help.
screen shot attached.

wp

  • Hero Member
  • *****
  • Posts: 11906
Re: help aligning components
« Reply #8 on: August 06, 2020, 01:29:35 am »
With both header labels, checkboxes and edit controls inside the same container, you also could use ChildSizing.

 

TinyPortal © 2005-2018