INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Need some Math help please

Need some Math help please

(OP)
Hi All, I have a DBGrid with 6-Columns and Widths of 30, 30, 50,130, 130, 130. When I make the Main Form wider or narrower, I want to keep the Column Widths proportional. I am at the limit of my Math skills and this is not working at all well. Can someone please tell me how to do this. There seems to be a Rounding problem as I keep getting varying white-space to the right of the last column.

CODE

procedure SetColumnWidths;
var
  i : Integer;
  aPcnt : Real;
  aWid : Real;
  anInc : Integer;
  Smaller : Boolean;
begin
  if aClientWidth = ClientWidth then Exit;
  Smaller:=aClientWidth > ClientWidth;
  aPcnt:=(((aClientWidth-ClientWidth) / ClientWidth) * 100.0);
  for i:=0 to dbgridItems.Columns.Count-1 do
  begin
    aWid:=dbgridItems.Columns[i].Width;
    anInc:=Abs(Round(aWid * aPcnt / 100.0));
    if Smaller then
      dbgridItems.Columns[i].Width:=dbgridItems.columns[i].Width-anInc else
      dbgridItems.Columns[i].Width:=dbgridItems.columns[i].Width+anInc;
  end;
  aClientWidth:=ClientWidth;
  aClientHeight:=ClientHeight;
end; 

RE: Need some Math help please

I'm not sure entirely what you are wanting to do, but to keep your TColumns proportional to the form in those ratios:

CODE

procedure TForm1.FormResize(Sender: TObject);
// resize DBGRid proportionally, sizes:  30, 30, 50,130, 130, 130
const
  DBGridPro: array[0..5] of integer = (30, 30, 50, 130, 130, 130);
var
  totalsize, i: integer;
begin
  // compute total size of grid proportions, can be done elsewhere
  totalsize := 0;
  for i := 0 to 5 do
    totalsize := totalsize + DBGridPro[i];
  // now resize each column porportionally.
  for i := 0 to 5 do
    DBGrid1.Columns.Items[i].Width := trunc(DBGrid1.Width * (DBGridPro[i] / totalsize));
end; 

Of course, you want to take the farthest left space into account, too. But this seems to work for me (except for the white space the controls always seem to produce when you size a part to the same dimension as the parent control).

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.

RE: Need some Math help please

(OP)
Thanks Glenn,

In the interim, I finished up doing something similar, albeit, almost by brute-force. Your approach is much more elegant so will use that with very grateful thanks for the help.

Dave

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close