Recent

Author Topic: Grids - Impossible Grids  (Read 879 times)

JLWest

  • Sr. Member
  • ****
  • Posts: 390
Grids - Impossible Grids
« on: January 17, 2019, 10:55:00 pm »


Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, eventlog, FileUtil,
  9.   Forms,   Controls, Graphics, Dialogs,
  10.   StdCtrls, ExtCtrls, Grids, StrUtils;
  11.  
  12. type
  13.  PData = ^TData;
  14.   TData = record
  15.   FLine  : String;
  16.   FIndex : Integer;
  17.   RCDId  : String[4];
  18.   ICAO   : String[8];
  19.   //other data if needed
  20.       end;
  21.  
  22.  
  23.   { TbtnPath }
  24.  
  25.   TbtnPath = class(TForm)
  26.     btnPath: TButton;
  27.     btnClose: TButton;
  28.     Edit1: TEdit;
  29.     Edit2: TEdit;
  30.     Edit3: TEdit;
  31.     Edit4: TEdit;
  32.     selected_file: TEdit;
  33.     edICAO: TEdit;
  34.     Label1: TLabel;
  35.     Label2: TLabel;
  36.     ldAptDotDat: TButton;
  37.     Path: TOpenDialog;
  38.     g1300Grid: TStringGrid;
  39.     G1301Grid: TStringGrid;
  40.     procedure btnCloseClick(Sender: TObject);
  41.     procedure btnPathClick(Sender: TObject);
  42.     procedure FormCreate(Sender: TObject);
  43.     procedure FormShow(Sender: TObject);
  44.     procedure ldAptDotDatClick(Sender: TObject);
  45.     procedure SetGrids;
  46.     procedure ProgramSetup;
  47.     function LoadDataFromFile(const AFileName: String): Boolean;
  48.  
  49.     function GetRecordCount(const AFileName: string): Longint;
  50.  
  51.  
  52.   private
  53.  
  54.   public
  55.  
  56.   end;
  57.  Const
  58.   // Live Data
  59.   //  C_FName = 'C:\Files\Apt.Dat';
  60.   //Test Data
  61.   //  C_FName = 'C:\FilesTest\Apt.Dat';
  62.     X_FName = 'C:\FilesTest\Apt.Dat';
  63. var
  64.   btnPath: TbtnPath;
  65.   Data: array of TData;
  66.   DataCondition : array of TData;
  67.   BlocKSize : LongInt = 0;
  68.   C_FName   : String = '';
  69.   FileName  : String = '';
  70.  
  71. implementation
  72.  
  73. {$R *.lfm}
  74.  
  75. procedure TbtnPath.FormCreate(Sender: TObject);
  76.  Var
  77.   Loc     : String = '';
  78.   PGMName : String = '';
  79. begin
  80.   loc := Application.Location;
  81.   PGMName := Application.ExeName;
  82.   Caption := 'APTDotDat Array Load ' + Loc + PGMName;
  83. end;
  84.  
  85. procedure TbtnPath.FormShow(Sender: TObject);
  86.   Var
  87.    Loc     : String = '';
  88.    PGMName : String = '';
  89.   begin
  90.    loc := Application.Location;
  91.    PGMName := Application.ExeName;
  92.    Caption := 'APTDotDat Array Load ' + Loc + PGMName;
  93.    ProgramSetup ;
  94.   end;
  95.  
  96. procedure TbtnPath.ProgramSetup;
  97.  begin
  98.   SetGrids;
  99.  end;
  100.  
  101. procedure TbtnPath.ldAptDotDatClick(Sender: TObject);
  102.  var
  103.   Len, I: Integer;
  104.   Valid : Boolean = False;
  105.   // Debug
  106.   LowNbr  : Longint = 0;
  107.   HighNbr : Longint = 0;
  108.   Item : String = '';
  109. begin
  110.  Data := nil;
  111.  DataCondition := nil;
  112.  Valid := FileExists(FileName);
  113.  if Not Valid then  begin
  114.    MessageDlg('Cannot load file! ', mtError, [mbOk], 0);
  115.    Exit;
  116.  end;
  117.  Valid := LoadDataFromFile(FileName);
  118.  // for some reason you wish to start from
  119.  // the last record,
  120.  // I saw it in one of your previous thread
  121.  
  122.  for I := Low(Data) to High(Data) do begin
  123.  
  124.    //check for some conditions, if the conditions are met copy
  125.    //the record/line to another array
  126.    //DataCondition(Listbox2,3 equivalent)
  127.    //you can define more arrays/records if needed)
  128.    Len := Length(DataCondition);
  129.    SetLength(DataCondition, Len + 1);
  130.    DataCondition[Len].FLine := Data[I].FLine;
  131.    // keep the original index for later reference,
  132.    // maybe you wish to delete from the file
  133.    DataCondition[Len].FIndex := Data[I].FIndex;
  134.   end;
  135.    HighNbr := High(Data);
  136.    Item := Data[HighNbr].Fline;
  137.    Edit1.Text := Item;
  138.    end;
  139.  
  140.  function TbtnPath.LoadDataFromFile(const AFileName: String): Boolean;
  141.   var
  142.    DataFile : TextFile;
  143.    Counter  : Integer = 0;
  144.    Line     : String = '';
  145.    RCD      : String = '';
  146.    Len      : Integer;
  147.    i        : Integer;
  148.    NeedtoExit : Boolean = False;
  149.     begin
  150.       Result := True;
  151.       AssignFile(DataFile, AFileName);
  152.       try
  153.         Reset(DataFile);
  154.         SetLength(Data, BlockSize); // set here
  155.         while not eof(DataFile) do
  156.         begin
  157.           Readln(DataFile, Line);
  158.           Data[Counter].ICAO := '';
  159.           Data[Counter].RCDId := Copy2Space(Line);
  160.           RCD := Data[Counter].RCDId;
  161.           Case RCD of
  162.            '1'  : Data[Counter].ICAO := ExtractWord(5, Line, [' ']);
  163.            '16' : Data[Counter].ICAO := ExtractWord(5, Line, [' ']);
  164.            '17' : Data[Counter].ICAO := ExtractWord(5, Line, [' ']);
  165.           end;
  166.           Data[Counter].FLine  := Line;
  167.           Data[Counter].FIndex := Len;
  168.           Inc(Counter);
  169.           Edit3.Text := intToStr(Counter);
  170.           Application.ProcessMessages;
  171.         end;
  172.         CloseFile(DataFile);
  173.        except
  174.          Result := False;
  175.        end;
  176.     end;
  177.  
  178. function TbtnPath.GetRecordCount(const AFileName: string): Longint;
  179. var
  180.   DataFile: TextFile;
  181.   LstRcd  : String = '';
  182. begin
  183.   Result := 0;
  184.   AssignFile(DataFile, AFileName);
  185.   try
  186.     Reset(DataFile);
  187.     while not eof(DataFile) do begin
  188.       // this line has changed, basically it
  189.       // reads nothing but is needed otherwise
  190.       // we go into an endless loop
  191.       Readln(DataFile);
  192.       Inc(Result);
  193.      end;
  194.     BlocKSize := Result;
  195.     CloseFile(DataFile);
  196.   except
  197.     //...
  198.   end;
  199.   Edit2.Text := IntToStr(BlockSize);
  200. end;
  201.  
  202. procedure TbtnPath.btnCloseClick(Sender: TObject);
  203. begin
  204.   Close;
  205. end;
  206.  
  207. procedure TbtnPath.SetGrids;
  208.  Var i : integer =-1;
  209.  Begin
  210.   with G1300Grid do begin
  211.     Clear;
  212.     ColWidths[0]  := 50;
  213.     //ColWidths[1]  := 50;
  214.     //ColWidths[2]  := 100;
  215.     //ColWidths[3]  := 100;
  216.     //ColWidths[4]  := 65;
  217.     //ColWidths[5]  := 100;
  218.     //ColWidths[6]  := 200;
  219.  
  220.     // Set the alignments
  221.       Columns[0].Alignment := taRightJustify;
  222. //    Align[1] := taRightJustify;
  223.  //   G1300Grid.Align[2] := taRightJustify;
  224.     //G1300Grid.Align[3] := taRightJustify;
  225.     //G1300Grid.Align[4] := taRightJustify;
  226.     //G1300Grid.Align[5] := taRightJustify;
  227.     //G1300Grid.Align[6] := taRightJustify;
  228.  
  229.         // Set the header's texts
  230.  
  231.  
  232.     // Set the header's alignments
  233.     for i := 0 to 6 do begin
  234.      Columns[i].Title.Alignment := taCenter;
  235.     end;
  236.  
  237.     Columns[0].Title.Caption := 'Ref:';
  238.     Columns[1].Title.Caption := 'RCID';
  239.     Columns[2].Title.Caption := 'Lat';
  240.     Columns[3].Title.Caption := 'Lon';
  241.     Columns[4].Title.Caption := 'Alt';
  242.     Columns[5].Title.Caption := 'Type';
  243.     Columns[6].Title.Caption := 'Info';
  244.  
  245.    end;
  246.  end;
  247.  
  248. procedure TbtnPath.btnPathClick(Sender: TObject);
  249. begin
  250.   If Path.Execute then begin selected_file.Caption:= Path.FileName; end;
  251.   FileName := Path.FileName;
  252.   if FileExists(FileName) then begin
  253.      BlockSize  := GetRecordCount(FileName);
  254.   end;
  255. end;
  256.  
  257. end.
  258.  

I get an out of bounds error at I think line 212. The table I'm trying to set up has 1 fixed row and 1 fixed column and I want 7 custom columns. Total of 9. So I comment out line 212 and try for the next line and get the same error.

Could someone take a look and point out the error of my ways?

Thanks
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
500 GB SSD
3 Terabyte conventional disk space.

wp

  • Hero Member
  • *****
  • Posts: 5444
Re: Grids - Impossible Grids
« Reply #1 on: January 17, 2019, 11:48:42 pm »
This is because when you "Clear" the grid in line 211 you clear everything. Before you continue you must specifiy the grid dimensions again, normally by setting ColCount and RowCount. But you seem to work with Columns, therefore you cannot set ColCount, but must create the required count of TGridColumn objects:
Code: Pascal  [Select]
  1. with G1300Grid do begin
  2.   Clear;
  3.   FixedCols := 1;
  4.   with Columns.Add do begin  // Columns[0]
  5.     Width := 50;
  6.     Title.Caption := 'Ref.';
  7.     Alignment := taRightJustify;
  8.   end;
  9.   // etc,

Or, if your "Clear" only wants to clear the data, you have less work when you set the RowCount equal to the FixedRows.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

howardpc

  • Hero Member
  • *****
  • Posts: 2915
Re: Grids - Impossible Grids
« Reply #2 on: January 17, 2019, 11:55:06 pm »
Or, since you have presumably added the columns in the designer, and show this form only once? -  don't call Clear at all in your OnShow event.
« Last Edit: January 17, 2019, 11:56:44 pm by howardpc »

JLWest

  • Sr. Member
  • ****
  • Posts: 390
Re: Grids - Impossible Grids
« Reply #3 on: January 18, 2019, 12:16:11 am »
@howardpc

"Or, since you have presumably added the columns in the designer, and show this form only once? -  don't call Clear at all in your OnShow event."

That's correct. It is setup in the designer and on the main form out of FormShow.

Commented out the Clear statement. Thought it just cleared the data in the grid.

Still get the same error. And I think at the same place.

Question: When I do the following:

  ColWidths[0]  := 50;

I think I'm referring to column 0 row zero. The fixed row and column, Or or is this wrong.

project attached if it's easier.

Thanks.
   
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
500 GB SSD
3 Terabyte conventional disk space.

josh

  • Hero Member
  • *****
  • Posts: 719
Re: Grids - Impossible Grids
« Reply #4 on: January 18, 2019, 12:24:58 am »
You could add the columns
Code: [Select]
with G1300Grid do begin
    //Clear;
     columns.Add;
    columns.Add;
    columns.Add;
    columns.Add;
    columns.Add;
    columns.Add;
    columns.Add;
    ColWidths[0]  := 50;
    ColWidths[1]  := 50;
    ColWidths[2]  := 100;
    ColWidths[3]  := 100;
    ColWidths[4]  := 65;
    ColWidths[5]  := 100;
    ColWidths[6]  := 200;         
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

JLWest

  • Sr. Member
  • ****
  • Posts: 390
Re: Grids - Impossible Grids
« Reply #5 on: January 18, 2019, 01:02:35 am »
@Josh

it's the   "ColWidths[0]  := 50;"  where the error is thrown. So I can't really use that.
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
500 GB SSD
3 Terabyte conventional disk space.

Blaazen

  • Hero Member
  • *****
  • Posts: 2770
  • POKE 54296,15
    • Eye-Candy Controls
Re: Grids - Impossible Grids
« Reply #6 on: January 18, 2019, 01:16:38 am »
The error is thrown because no columns exist. Josh's code adds columns. So yes, you can really use that.
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

JLWest

  • Sr. Member
  • ****
  • Posts: 390
Re: Grids - Impossible Grids
« Reply #7 on: January 18, 2019, 01:45:53 am »
I trieed Josh code and still got the error.

The columns are set up in the object inspector.

G1300Grid.ColCount 9
G1300Grid.FixedCols 1
G1300Grid.FixedRows 1
G1300Grid.RowCount 10

Prior to executing I'm looking at a grid with 1 fixed row and 1 fixed column and 7 regular or custom column.

Are you suggesting I set everything to 0 in the object inspector and then add it all back in code.


« Last Edit: January 18, 2019, 01:48:45 am by JLWest »
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
500 GB SSD
3 Terabyte conventional disk space.

josh

  • Hero Member
  • *****
  • Posts: 719
Re: Grids - Impossible Grids
« Reply #8 on: January 18, 2019, 02:05:41 am »
Hi

Adding the columns.add to your uploaded project works fine here.

By setting col count to 9 will not set columns, you can do this in code, or using the object inspector for the grid.
Hope the pic helps

Note you will need to add one more column than is in the pic for your proj to run.
« Last Edit: January 18, 2019, 02:12:17 am by josh »
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

Blaazen

  • Hero Member
  • *****
  • Posts: 2770
  • POKE 54296,15
    • Eye-Candy Controls
Re: Grids - Impossible Grids
« Reply #9 on: January 18, 2019, 02:06:19 am »
As mentioned by wp, you can't just set ColCount property. You need to create Columns by Columns property in OI (where you click the [...] button) or by code.
(It is the feature of TStringGrid that it allows two ways of working with columns.)
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

JLWest

  • Sr. Member
  • ****
  • Posts: 390
Re: Grids - Impossible Grids
« Reply #10 on: January 18, 2019, 02:19:12 am »
I think I just got it to work as far as FixedRows and FixedColumns. Custom Columns and headings and maybe headings and alignments and Column width.
I changed to the following: Columns[0].Width := 100;  Move the   FixedRows and FixedColumns to the end of the routine to avoide an error.

Only took two days.

Thanks
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
500 GB SSD
3 Terabyte conventional disk space.