home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0172_An Improved StringGrid Component.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  5.4 KB  |  259 lines

  1. (*
  2.  DESCRIPTION :  A improved version of the stringgrid component
  3.  AUTHOR      : Harm v. Zoest, email : 4923559@hsu1.HVU.nl
  4.  VERSION     :  0.95 (beta) 06-27- 1996
  5.  REMARKS     : If you have comments, found bugs, ore you have added some
  6.                nice features, please mail me!
  7.  *)
  8.  
  9. {$S-,I-,D-,L-}
  10. unit ImpGrid;
  11.  
  12. interface
  13.  
  14. uses
  15.    WinTypes, SysUtils, Messages, Classes, Controls, Grids;
  16.  
  17. type
  18.   { own exeptions}}
  19.   EErrorInCell = class(Exception);
  20.   EFileNotFound = class(Exception);
  21.  
  22.  
  23.   TImpGrid = class(TStringGrid)
  24.   private
  25.     FHCol, FHRow: TStrings;
  26.     procedure InitHCol;
  27.     procedure InitHRow;
  28.   protected
  29.     procedure Loaded; override;
  30.   published
  31.     property HCol: TStrings read FHCol write SetHCol;
  32.     property HRow: TStrings read FHRow write SetHRow;
  33.   public
  34.     constructor Create(AOwner: TComponent); override;
  35.     procedure RemoveRows(RowIndex, RCount: LongInt);
  36.     procedure InsertRows(RowIndex, RCount: LongInt);
  37.     procedure RemoveCols(ColIndex, CCount: LongInt);
  38.     procedure InsertCols(ColIndex, CCount: LongInt);
  39.     procedure Clear;
  40.     function isCell(SubStr: String; var ACol, ARow: LongInt): Boolean;
  41.     procedure SaveToFile(FileName: String);
  42.     procedure LoadFromFile(FileName: String);
  43.     function CellToReal(ACol, ARow: LongInt): Real;
  44.   end;
  45.  
  46. procedure Register;
  47.  
  48. implementation
  49.  
  50.  
  51. constructor TImpGrid.Create(AOwner: TComponent);
  52. begin
  53.   inherited Create(AOwner);
  54.   FHCol:=TStringList.Create;
  55.   FHRow:=TStringList.Create;
  56. end;
  57.  
  58. procedure Timpgrid.Loaded;
  59. begin
  60.   inherited Loaded;
  61.   initHCol;
  62.   initHRow;
  63. end;
  64.  
  65. procedure TImpGrid.SetHCol(Value: TStrings);
  66. begin
  67.   FHCol.Assign(Value);
  68.   InitHCol;
  69.   Refresh;
  70. end;
  71.  
  72. procedure TImpGrid.SetHRow(Value: TStrings);
  73. begin
  74.   FHRow.Assign(Value);
  75.   InitHRow;
  76.   Refresh;
  77. end;
  78.  
  79. procedure TImpgrid.InitHCol;
  80. var
  81.   I: Integer;
  82. begin
  83.   if (FHCol <> nil) then
  84.     for I :=0 to pred( ColCount) do
  85.     begin
  86.       if I <FHCol.Count then
  87.          Cells[I, 0] :=FHCol[I]
  88.       else Cells[I, 0] :='';
  89.     end;{for}
  90. end;
  91.  
  92. procedure ImpGrid.InitHRow;
  93. var
  94.   I: Integer;
  95. begin
  96.   if (FHRow <> nil) then
  97.     for I :=0 to RowCount -2 do
  98.     begin
  99.       if I <FHRow.Count then
  100.       Cells[0, I + 1]:=FHRow[I]
  101.       else Cells[0, I + 1]:='';
  102.     end;
  103. end;
  104.  
  105. procedure TImpGrid.RemoveRows(RowIndex, RCount : LongInt);
  106. var
  107.   i: LongInt;
  108. begin
  109.   for i := RowIndex to RowCount - 1 do
  110.       Rows[i] := Rows[i + RCount];
  111.   RowCount := RowCount - RCount;
  112. end;
  113.  
  114.  
  115. procedure TImpGrid.InsertRows(RowIndex, RCount : LongInt);
  116. var
  117.   i: LongInt;
  118. begin
  119.   RowCount := RowCount + RCount;
  120.   for i := RowCount - 1 downto RowIndex do
  121.       Rows[i] := Rows[i - RCount];
  122. end;
  123.  
  124.  
  125. procedure TImpGrid.RemoveCols(ColIndex, CCount : LongInt);
  126. var
  127.   i: LongInt;
  128. begin
  129.   for i := ColIndex to ColCount - 1 do
  130.       Cols[i] := Cols[i + CCount];
  131.   ColCount := ColCount - CCount;
  132. end;
  133.  
  134.  
  135. procedure TImpGrid.InsertCols(ColIndex, CCount : LongInt);
  136. var
  137.   i: LongInt;
  138. begin
  139.   ColCount := ColCount + CCount;
  140.   for i := ColCount - 1 downto ColIndex do
  141.       Cols[i] := Cols[i - CCount];
  142. end;
  143.  
  144.  
  145. procedure TImpGrid.Clear;
  146. var
  147.   i: LongInt;
  148. begin
  149.   for i:= 0 to ColCount - 1 do
  150.       Cols[i].Clear;
  151. end;
  152.  
  153.  
  154. function TImpGrid.isCell(SubStr: String; var ACol, ARow: LongInt): Boolean;
  155. var
  156.   i, j: LongInt;
  157. begin
  158.   for i := 0 to RowCount - 1 do
  159.   begin
  160.     for j := 0 to ColCount - 1 do
  161.     begin
  162.       if Rows[i].Strings[j] = SubStr then
  163.       begin
  164.         ARow := i;
  165.         ACol := j;
  166.         Result := True;
  167.         exit;
  168.       end;
  169.     end;
  170.   end;
  171.   Result := False;
  172. end;
  173.  
  174.  
  175. procedure TImpGrid.SaveToFile(FileName: String);
  176. var
  177.   i, j: LongInt;
  178.   ss: string;
  179.   f: TextFile;
  180. begin
  181.   AssignFile(f, FileName);
  182.   Rewrite(f);
  183.   ss := IntToStr(ColCount) + ',' + IntToStr(RowCount);
  184.   Writeln(f, ss);
  185.  
  186.   for i := 0 to RowCount - 1 do
  187.   begin
  188.     for j := 0 to ColCount - 1 do
  189.     begin
  190.       if Cells[j, i] <> '' then
  191.       begin
  192.         ss := IntToStr(j) + ',' + IntToStr(i) + ',' + Cells[j, i];
  193.         Writeln(f, ss);
  194.       end;
  195.     end;
  196.   end;
  197.   CloseFile(f);
  198. end;
  199.  
  200.  
  201. procedure TImpGrid.LoadFromFile(FileName: String);
  202. var
  203.   X, Y: Integer;
  204.   ss, ss1: string;
  205.   f: TextFile;
  206. begin
  207.   AssignFile(f, FileName);
  208.   Reset(f);
  209.   if IOResult <> 0 then raise EFileNotFound.Create('File ' + FileName + ' not found');
  210.   Readln(f, ss);
  211.   if ss <> '' then
  212.   begin
  213.     ss1 := Copy(ss, 1, Pos(',', ss) - 1);
  214.     ColCount := StrToInt(ss1);
  215.     ss1 := Copy(ss, Pos(',', ss) + 1, Length(ss));
  216.     RowCount := StrToInt(ss1);
  217.   end;
  218.  
  219.   while not Eof(f) do
  220.   begin
  221.     Readln(f, ss);
  222.     ss1 := Copy(ss, 1, Pos(',', ss) - 1);
  223.     ss := Copy(ss, Pos(',', ss) + 1, Length(ss));
  224.     X := StrToInt(ss1);
  225.     ss1 := Copy(ss, 1, Pos(',', ss) - 1);
  226.     ss := Copy(ss, Pos(',', ss) + 1, Length(ss));
  227.     Y := StrToInt(ss1);
  228.     Cells[X, Y] := ss;
  229.   end;
  230.   CloseFile(f);
  231. end;
  232.  
  233.  
  234. function TImpGrid.CellToReal(ACol, ARow: LongInt): Real;
  235. var
  236.   i: Real;
  237.   Code: Integer;
  238. begin
  239.   if Cells[ACol, ARow] <> '' then
  240.   begin
  241.     Val(Cells[ACol, ARow], i, Code);
  242.     if Code <> 0 then raise
  243.           EErrorInCell.Create('Error at position: ' +
  244.           IntToStr(Code) + ' in Cell [' + IntToStr(ACol) + ', ' +
  245.           IntToStr(ARow) + '].')
  246.     else
  247.     Result := i;
  248.   end;
  249. end;
  250.  
  251.  
  252. procedure Register;
  253. begin
  254.   RegisterComponents('Improved Components', [TImpGrid]);
  255. end;
  256.  
  257.  
  258. end.
  259.