Evo... nisam imao vremena da napravim nesto bolje, ali ovo otprilike radi posao.
Code:
unit NewStringGrid;
interface
uses
SysUtils, Classes, Controls, Grids;
type
TNewStringGrid = class(TStringGrid)
private
{ Private declarations }
p_Cols:array of Integer;
p_ColCount:Integer;
lstStrings:TStringList;
procedure Set_ColCount(I:Integer);
function Get_ColCount:Integer;
procedure Set_Cols(I:Integer; Val:Integer);
function Get_Cols(I:Integer):Integer;
procedure CalculateFields;
protected
{ Protected declarations }
public
{ Public declarations }
function LoadStringFile( strFileName: String ):Boolean;
constructor Create( AOwner: TComponent ); override;
destructor Destroy; override;
property Colunms[I:Integer]:Integer read Get_Cols write Set_Cols;
published
{ Published declarations }
property ColunmCount:Integer read Get_ColCount write Set_ColCount;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TNewStringGrid]);
end;
{ TNewStringGrid }
procedure TNewStringGrid.CalculateFields;
var C:Integer;
X, Y: Integer;
S:String;
begin
For Y := 0 to lstStrings.Count-1 do
begin
C := 1;
For X:= 0 to p_ColCount-1 do
begin
S := Copy( lstStrings.Strings[Y], C, p_Cols[X] );
Inc( C, p_Cols[X] );
Cells[X,Y] := S;
end;
end;
end;
constructor TNewStringGrid.Create(AOwner: TComponent);
begin
inherited Create( AOwner );
FixedCols := 0;
FixedRows := 0;
RowCount := 1;
ColunmCount := 1;
lstStrings := TStringList.Create;
end;
destructor TNewStringGrid.Destroy;
begin
lstStrings.Free;
ColunmCount := 0;
inherited;
end;
function TNewStringGrid.Get_ColCount: Integer;
begin
Result := p_ColCount;
end;
function TNewStringGrid.Get_Cols(I: Integer): Integer;
begin
Result := p_Cols[I];
end;
function TNewStringGrid.LoadStringFile(strFileName: String): Boolean;
var F:System.Text;
S:String;
begin
Result := False;
System.Assign( F, strFileName );
Reset( F );
While Not Eof( F ) do
begin
ReadLn( F, S );
lstStrings.Add( S );
end;
System.Close( F );
RowCount := lstStrings.Count;
CalculateFields;
Result := True;
end;
procedure TNewStringGrid.Set_ColCount(I: Integer);
begin
p_ColCount := I;
SetLength( p_Cols, I );
ColCount := I;
end;
procedure TNewStringGrid.Set_Cols(I, Val: Integer);
begin
p_Cols[I] := Val;
CalculateFields;
end;
end.
Ubacis ovu kontrolu u formu, podesis koliko koja kolona ima karaktera (nije mi se dalo da napravim graficki prikaz toga tako da ces sa tim morati sam da se patis :P) i ucitas fajl. Posle mozes da menjas koliko je koja kolona siroka (u karakterima) i grid ce ponovo da rasporedi text po kolonama. Dodao sam 2 nova property-a (bio bi dovoljan i samo jedan, mogao sam da samo malo promenim ColCount umesto ubacivanja novog). Colunms je ono sto odredjuje koliko se karaktera nalazi u kojoj koloni. Znaci ako je Colunms[0]:=4, Colunms[1]:=5 kada se fajl ucita u prvoj koloni ce biti prva 4 karaktera, a u drugoj naredinh 5 i tako. Fajl ucitavas funkcijom LoadStringFile. Vidis kako otprilike to radi pa napravi neku svoju lepu kontrolicu koja ce sve da radi kako treba, da ima podrsku za exception-e, da ucitava fajl i sredjuje prikaz u design time-u,itd... nije tesko samo treba vremena... bas ono sto ja nemam :P