Recent

Author Topic: My package error  (Read 5871 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
My package error
« on: June 20, 2011, 01:14:45 pm »
Why this error in my package code

Error creating component: TExtDBListBox Access violation

Code: [Select]
unit ExtDBListBox;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Grids, sqldb, db;

type
  TExtDBListBox = class(TStringGrid)
  private
    { Private declarations }
    MyDataSource: TDataSource;
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(TheOwner: TComponent); override;
    procedure Requery();
    procedure ColumnVisible(ColIndex: integer; Value: boolean);
    procedure ColumnAlign(ColIndex: integer; Value: integer);
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  {$I ExtDBListBox_icon.lrs}
  RegisterComponents('Standard',[TExtDBListBox]);
end;

constructor TExtDBListBox.Create(TheOwner: TComponent);
begin
     inherited Create(TheOwner);
     {cambio il layout della griglia per far si che si veda come se fosse una listbox}
     Self.ColCount:=1;
     Self.FixedCols:=0;
     Self.FixedRows:=0;
     Self.AutoEdit:=false;
     Self.GridLineWidth:=0;
     Self.Requery(); //disegno la griglia
end;

procedure TExtDBListBox.Requery();
var
   NumRighe: LongInt;
   Colonna, Riga: integer;
begin
     Self.Clear; //pulisco la listbox
     {ora disegno il dataset}
     if not MyDataSource.DataSet.EOF then
     begin
          {mi ricavo il numero di colonne che devo stampare a video}
          Self.Col:=MyDataSource.DataSet.FieldCount;
          {mi scorro velocemente il recordset per capire quante righe ho}
          MyDataSource.DataSet.First;
          NumRighe:=0;
          while not MyDataSource.DataSet.EOF do
          begin
               Inc(NumRighe);
               MyDataSource.DataSet.Next;
          end;
          {ora che ho il numero di righe mi stampo la griglia}
          Self.Row:=NumRighe+1;
          {ora inserisco la testata}
          for Colonna:=0 to Self.Col-1 do
          begin
               Self.Cells[Colonna,0]:=MyDataSource.DataSet.Fields[Colonna].Name;
          end;
          {ora inserisco i dati}
          Riga:=1;
          MyDataSource.DataSet.First;
          while not MyDataSource.DataSet.EOF do
          begin
               {disegno la singola riga}
               for Colonna:=0 to Self.Col-1 do
               begin
                    Self.Cells[Colonna,Riga]:=MyDataSource.DataSet.Fields[Colonna].AsString;
               end;
               Inc(Riga);
               MyDataSource.DataSet.Next;
          end;
     end;
end;

procedure TExtDBListBox.ColumnVisible(ColIndex: integer; Value: boolean);
begin
     Self.Columns[ColIndex].Visible:=Value;
end;

procedure TExtDBListBox.ColumnAlign(ColIndex: integer; Value: integer);
begin
     if Value<0 then
        Self.Columns[ColIndex].Alignment:=taLeftJustify
     else if Value=0 then
          Self.Columns[ColIndex].Alignment:=taCenter
     else
         Self.Columns[ColIndex].Alignment:=taRightJustify;
end;



end.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: My package error
« Reply #1 on: June 20, 2011, 01:16:04 pm »
This error happens when I insert the new component on a form. The compilation did not give errors. Why I'm missing some information?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: My package error
« Reply #2 on: June 20, 2011, 02:40:21 pm »
I resolved, I was missing the dependency to LCL.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018