Forum > Beginners
Grids - Impossible Grids
JLWest:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, eventlog, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, Grids, StrUtils; type PData = ^TData; TData = record FLine : String; FIndex : Integer; RCDId : String[4]; ICAO : String[8]; //other data if needed end; { TbtnPath } TbtnPath = class(TForm) btnPath: TButton; btnClose: TButton; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; selected_file: TEdit; edICAO: TEdit; Label1: TLabel; Label2: TLabel; ldAptDotDat: TButton; Path: TOpenDialog; g1300Grid: TStringGrid; G1301Grid: TStringGrid; procedure btnCloseClick(Sender: TObject); procedure btnPathClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormShow(Sender: TObject); procedure ldAptDotDatClick(Sender: TObject); procedure SetGrids; procedure ProgramSetup; function LoadDataFromFile(const AFileName: String): Boolean; function GetRecordCount(const AFileName: string): Longint; private public end; Const // Live Data // C_FName = 'C:\Files\Apt.Dat'; //Test Data // C_FName = 'C:\FilesTest\Apt.Dat'; X_FName = 'C:\FilesTest\Apt.Dat';var btnPath: TbtnPath; Data: array of TData; DataCondition : array of TData; BlocKSize : LongInt = 0; C_FName : String = ''; FileName : String = ''; implementation {$R *.lfm} procedure TbtnPath.FormCreate(Sender: TObject); Var Loc : String = ''; PGMName : String = '';begin loc := Application.Location; PGMName := Application.ExeName; Caption := 'APTDotDat Array Load ' + Loc + PGMName;end; procedure TbtnPath.FormShow(Sender: TObject); Var Loc : String = ''; PGMName : String = ''; begin loc := Application.Location; PGMName := Application.ExeName; Caption := 'APTDotDat Array Load ' + Loc + PGMName; ProgramSetup ; end; procedure TbtnPath.ProgramSetup; begin SetGrids; end; procedure TbtnPath.ldAptDotDatClick(Sender: TObject); var Len, I: Integer; Valid : Boolean = False; // Debug LowNbr : Longint = 0; HighNbr : Longint = 0; Item : String = '';begin Data := nil; DataCondition := nil; Valid := FileExists(FileName); if Not Valid then begin MessageDlg('Cannot load file! ', mtError, [mbOk], 0); Exit; end; Valid := LoadDataFromFile(FileName); // for some reason you wish to start from // the last record, // I saw it in one of your previous thread for I := Low(Data) to High(Data) do begin //check for some conditions, if the conditions are met copy //the record/line to another array //DataCondition(Listbox2,3 equivalent) //you can define more arrays/records if needed) Len := Length(DataCondition); SetLength(DataCondition, Len + 1); DataCondition[Len].FLine := Data[I].FLine; // keep the original index for later reference, // maybe you wish to delete from the file DataCondition[Len].FIndex := Data[I].FIndex; end; HighNbr := High(Data); Item := Data[HighNbr].Fline; Edit1.Text := Item; end; function TbtnPath.LoadDataFromFile(const AFileName: String): Boolean; var DataFile : TextFile; Counter : Integer = 0; Line : String = ''; RCD : String = ''; Len : Integer; i : Integer; NeedtoExit : Boolean = False; begin Result := True; AssignFile(DataFile, AFileName); try Reset(DataFile); SetLength(Data, BlockSize); // set here while not eof(DataFile) do begin Readln(DataFile, Line); Data[Counter].ICAO := ''; Data[Counter].RCDId := Copy2Space(Line); RCD := Data[Counter].RCDId; Case RCD of '1' : Data[Counter].ICAO := ExtractWord(5, Line, [' ']); '16' : Data[Counter].ICAO := ExtractWord(5, Line, [' ']); '17' : Data[Counter].ICAO := ExtractWord(5, Line, [' ']); end; Data[Counter].FLine := Line; Data[Counter].FIndex := Len; Inc(Counter); Edit3.Text := intToStr(Counter); Application.ProcessMessages; end; CloseFile(DataFile); except Result := False; end; end; function TbtnPath.GetRecordCount(const AFileName: string): Longint;var DataFile: TextFile; LstRcd : String = '';begin Result := 0; AssignFile(DataFile, AFileName); try Reset(DataFile); while not eof(DataFile) do begin // this line has changed, basically it // reads nothing but is needed otherwise // we go into an endless loop Readln(DataFile); Inc(Result); end; BlocKSize := Result; CloseFile(DataFile); except //... end; Edit2.Text := IntToStr(BlockSize);end; procedure TbtnPath.btnCloseClick(Sender: TObject);begin Close;end; procedure TbtnPath.SetGrids; Var i : integer =-1; Begin with G1300Grid do begin Clear; ColWidths[0] := 50; //ColWidths[1] := 50; //ColWidths[2] := 100; //ColWidths[3] := 100; //ColWidths[4] := 65; //ColWidths[5] := 100; //ColWidths[6] := 200; // Set the alignments Columns[0].Alignment := taRightJustify;// Align[1] := taRightJustify; // G1300Grid.Align[2] := taRightJustify; //G1300Grid.Align[3] := taRightJustify; //G1300Grid.Align[4] := taRightJustify; //G1300Grid.Align[5] := taRightJustify; //G1300Grid.Align[6] := taRightJustify; // Set the header's texts // Set the header's alignments for i := 0 to 6 do begin Columns[i].Title.Alignment := taCenter; end; Columns[0].Title.Caption := 'Ref:'; Columns[1].Title.Caption := 'RCID'; Columns[2].Title.Caption := 'Lat'; Columns[3].Title.Caption := 'Lon'; Columns[4].Title.Caption := 'Alt'; Columns[5].Title.Caption := 'Type'; Columns[6].Title.Caption := 'Info'; end; end; procedure TbtnPath.btnPathClick(Sender: TObject);begin If Path.Execute then begin selected_file.Caption:= Path.FileName; end; FileName := Path.FileName; if FileExists(FileName) then begin BlockSize := GetRecordCount(FileName); end;end; end.
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
wp:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---with G1300Grid do begin Clear; FixedCols := 1; with Columns.Add do begin // Columns[0] Width := 50; Title.Caption := 'Ref.'; Alignment := taRightJustify; end; // etc,
Or, if your "Clear" only wants to clear the data, you have less work when you set the RowCount equal to the FixedRows.
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.
JLWest:
@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.
Josh:
You could add the columns
--- Code: --- 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;
--- End code ---
Navigation
[0] Message Index
[#] Next page