Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some Math help please 1

Status
Not open for further replies.

DaveWin31

Programmer
Dec 6, 2008
7
US
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;

 
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.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top