unit test;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, StdCtrls;
type
TfrmMain = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
oList:TStringList;
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.DFM}
{Automatically re-sizes string grid columns for best fit.}
procedure GridColAutoSize( Grid:TStringGrid );
var
nCol, nRow, nWidth, nMaxWidth: integer;
begin
with Grid as TStringGrid do begin
for nCol := FixedCols to ColCount - 1 do begin
nMaxWidth := 0;
for nRow := 0 to RowCount - 1 do begin
nWidth := Canvas.TextWidth( Cells[nCol,nRow] );
if nWidth > nMaxWidth then nMaxWidth := nWidth;
end;
ColWidths[nCol] := nMaxWidth + 7;
end;
end;
end;
{Update a specified grid row from a string list -- with adjustments.}
procedure UpdateGrid( AGrid:TStringGrid; ARow:integer; AList:TStringList );
begin
// If item[7] is not a single character, insert an extra column for alignment
if Length(AList[7]) <> 1 then AList.Insert(7,'');
// If AList has 14 items, concatenate 12 and 13 as one
if AList.Count = 14 then
begin
AList[12] := AList[12] + ' ' + AList[13];
AList.Delete(13);
end;
with AGrid do begin
// If grid is not large enough, adjust row and column counts.
if RowCount < (ARow+1) then RowCount := ARow+1;
if ColCount < (AList.Count+1) then (ColCount := AList.Count);
Rows[ARow].Assign(AList);
end;
GridColAutoSize( AGrid );
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
oList := TStringList.Create;
end;
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
oList.Free;
end;
procedure TfrmMain.Button1Click(Sender: TObject);
begin
oList.CommaText := 'A301 --> N02 03/12/03 18:11 00:00:05 ST 08006926002............... M 00:00 ................ PDQ.............';
UpdateGrid( StringGrid1, 1, oList );
end;
procedure TfrmMain.Button2Click(Sender: TObject);
begin
oList.CommaText := ' A207 --> N02 03/12/03 18:18 00:04:33 ST I 07966323260............... M 00:00 ................ Anne Garbutt....';
UpdateGrid( StringGrid1, 2, oList );
end;
procedure TfrmMain.Button3Click(Sender: TObject);
begin
oList.CommaText := ' A209 --> N01 03/12/03 18:21 00:02:38 ST I 08456070200............... M 00:00 ................ Lucy Earle......';
UpdateGrid( StringGrid1, 3, oList );
end;
end.