Recent

Author Topic: Question for inheriting a new component (SOLVED)  (Read 3752 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Question for inheriting a new component (SOLVED)
« on: June 21, 2011, 03:12:52 pm »
In creating a new component that inherits from an existing one as in my case below in which I create a new component by inheriting from a TStringGrid how do I make that when I click on a cell can perform the operation, what is the procedure or function to do this.

For example this code as I do if someone clicks on a cell to create a

ShowMessage ('OK');

In that procedure / function?



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 }
    MyDataSourceList: TDataSource; { il datasource che mi fa vedere i dati nel componente }
    MyDataSourceIn: TDataSource; { il datasource che contiene i record sorgenti }
    MyDataSourceOut: TDataSource; { il datasource che contiene i record in output, ovvero quelli che vado a modificare }

    Intestazione: boolean; { se TRUE vuol dire che devo far vedere l'intestazione dei dati nel componente }
  protected
    { Protected declarations }
    procedure SetDataSourceList(DataSourceList: TDataSource);
    procedure SetDataSourceIn(DataSourceIn: TDataSource);
    procedure SetDataSourceOut(DataSourceOut: TDataSource);
    procedure SetTitleColumns(TitleColumns: BOOLEAN);
    //procedure PrepareCanvas(aCol, aRow: integer; aState: TGridDrawState);
  public
    { Public declarations }
    constructor Create(TheOwner: TComponent); override;
    procedure Requery();
    procedure ColumnVisible(ColIndex: integer; Value: boolean);
    procedure ColumnAlign(ColIndex: integer; Value: integer);
    //procedure PrepareCanvas(aCol, aRow: Integer{; aState: TGridDrawState});
  published
    { Published declarations }
    property DataSourceList: TDataSource read MyDataSourceList write MyDataSourceList;
    property DataSourceIn: TDataSource read MyDataSourceIn write MyDataSourceIn;
    property DataSourceOut: TDataSource read MyDataSourceOut write MyDataSourceOut;
    property TitleColumns: BOOLEAN read Intestazione write Intestazione;
  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;
     Intestazione:=FALSE;

   {  Self.SetDataSourceList(nil); //mi prendo il datasource dal quale lavorare
     Self.SetDataSourceIn(nil); //mi prendo il datasource dal quale lavorare
     Self.SetDataSourceOut(nil); //mi prendo il datasource dal quale lavorare
    }

end;

procedure TExtDBListBox.Requery();
var
   NumRighe, NumColonne: LongInt;
   Colonna, Riga: integer;
   app:string;
   MyTextStyle: TTextStyle;
begin
     Self.SetDataSourceList(DataSourceList); //mi prendo il datasource dal quale lavorare
     Self.SetDataSourceIn(DataSourceIn); //mi prendo il datasource dal quale lavorare
     Self.SetDataSourceOut(DataSourceOut); //mi prendo il datasource dal quale lavorare

     Self.Clear; //pulisco la listbox
     {ora disegno il dataset}
     if ((MyDataSourceList<>nil)AND(not MyDataSourceList.DataSet.EOF)) then
     begin
          {mi ricavo il numero di colonne che devo stampare a video}
          NumColonne:=MyDataSourceList.DataSet.FieldCount;
          Self.ColCount:=NumColonne;
          {mi scorro velocemente il recordset per capire quante righe ho}
          MyDataSourceList.DataSet.First;
          NumRighe:=0;
          while not MyDataSourceList.DataSet.EOF do
          begin
               Inc(NumRighe);
               MyDataSourceList.DataSet.Next;
          end;
          {ora che ho il numero di righe mi stampo la griglia}
          if Intestazione=TRUE then
          begin
               Self.RowCount:=NumRighe+1;
               {ora inserisco la testata}
               for Colonna:=0 to NumColonne-1 do
               begin
                    app:=MyDataSourceList.DataSet.Fields[Colonna].Name;
                    if Length(Trim(app))<=0 then
                       app:='Col' + IntToStr(Colonna);
                    Self.Cells[Colonna,0]:=app;
               end;
               Riga:=1;
          end
          else
          begin
               //Self.Columns.Add.Create(nil);
               Self.RowCount:=NumRighe;
               Riga:=0;
          end;
          {ora inserisco i dati}
          MyDataSourceList.DataSet.First;
          while not MyDataSourceList.DataSet.EOF do
          begin
               {disegno la singola riga}
               for Colonna:=0 to NumColonne-1 do
               begin
                    Self.Cells[Colonna,Riga]:=MyDataSourceList.DataSet.Fields[Colonna].AsString;
               end;
               Inc(Riga);
               MyDataSourceList.DataSet.Next;
          end;
     end;
     Self.AutoSizeColumns; //dimensiona le colonne correttamente
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;

procedure TExtDBListBox.SetDataSourceList(DataSourceList: TDataSource);
begin
     Self.MyDataSourceList:=DataSourceList;
end;

procedure TExtDBListBox.SetDataSourceIn(DataSourceIn: TDataSource);
begin
     Self.MyDataSourceIn:=DataSourceIn;
end;

procedure TExtDBListBox.SetDataSourceOut(DataSourceOut: TDataSource);
begin
     Self.MyDataSourceOut:=DataSourceOut;
end;

procedure TExtDBListBox.SetTitleColumns(TitleColumns: BOOLEAN);
begin
     Self.Intestazione:=TitleColumns;
end;

{procedure TExtDBListBox.PrepareCanvas(aCol, aRow: integer; aState: TGridDrawState);
var
   a, b: integer;
begin
     a:=0;
     a:=b;
end;}

end.

« Last Edit: June 21, 2011, 04:03:27 pm by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Question for inheriting a new component
« Reply #1 on: June 21, 2011, 04:03:09 pm »
Solved with this code

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 }
    MyDataSourceList: TDataSource; { il datasource che mi fa vedere i dati nel componente }
    MyDataSourceIn: TDataSource; { il datasource che contiene i record sorgenti }
    MyDataSourceOut: TDataSource; { il datasource che contiene i record in output, ovvero quelli che vado a modificare }

    Intestazione: boolean; { se TRUE vuol dire che devo far vedere l'intestazione dei dati nel componente }
  protected
    { Protected declarations }
    procedure SetDataSourceList(DataSourceList: TDataSource);
    procedure SetDataSourceIn(DataSourceIn: TDataSource);
    procedure SetDataSourceOut(DataSourceOut: TDataSource);
    procedure SetTitleColumns(TitleColumns: BOOLEAN);
    //procedure PrepareCanvas(aCol, aRow: integer; aState: TGridDrawState);
  public
    { Public declarations }
    constructor Create(TheOwner: TComponent); override;
    procedure Requery();
    procedure ColumnVisible(ColIndex: integer; Value: boolean);
    procedure ColumnAlign(ColIndex: integer; Value: integer);
    procedure Click; override;
    //procedure PrepareCanvas(aCol, aRow: Integer{; aState: TGridDrawState});
  published
    { Published declarations }
    property OnClick;
    property DataSourceList: TDataSource read MyDataSourceList write MyDataSourceList;
    property DataSourceIn: TDataSource read MyDataSourceIn write MyDataSourceIn;
    property DataSourceOut: TDataSource read MyDataSourceOut write MyDataSourceOut;
    property TitleColumns: BOOLEAN read Intestazione write Intestazione;
  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;
     Intestazione:=FALSE;
     Self.SetDataSourceList(nil); //mi prendo il datasource dal quale lavorare
     Self.SetDataSourceIn(nil); //mi prendo il datasource dal quale lavorare
     Self.SetDataSourceOut(nil); //mi prendo il datasource dal quale lavorare
end;

procedure TExtDBListBox.Requery();
var
   NumRighe, NumColonne: LongInt;
   Colonna, Riga: integer;
   app:string;
   MyTextStyle: TTextStyle;
begin
     Self.SetDataSourceList(DataSourceList); //mi prendo il datasource dal quale lavorare
     Self.SetDataSourceIn(DataSourceIn); //mi prendo il datasource dal quale lavorare
     Self.SetDataSourceOut(DataSourceOut); //mi prendo il datasource dal quale lavorare

     Self.Clear; //pulisco la listbox
     {ora disegno il dataset}
     if ((MyDataSourceList<>nil)AND(not MyDataSourceList.DataSet.EOF)) then
     begin
          {mi ricavo il numero di colonne che devo stampare a video}
          NumColonne:=MyDataSourceList.DataSet.FieldCount;
          Self.ColCount:=NumColonne;
          {mi scorro velocemente il recordset per capire quante righe ho}
          MyDataSourceList.DataSet.First;
          NumRighe:=0;
          while not MyDataSourceList.DataSet.EOF do
          begin
               Inc(NumRighe);
               MyDataSourceList.DataSet.Next;
          end;
          {ora che ho il numero di righe mi stampo la griglia}
          if Intestazione=TRUE then
          begin
               Self.RowCount:=NumRighe+1;
               {ora inserisco la testata}
               for Colonna:=0 to NumColonne-1 do
               begin
                    app:=MyDataSourceList.DataSet.Fields[Colonna].Name;
                    if Length(Trim(app))<=0 then
                       app:='Col' + IntToStr(Colonna);
                    Self.Cells[Colonna,0]:=app;
               end;
               Riga:=1;
          end
          else
          begin
               //Self.Columns.Add.Create(nil);
               Self.RowCount:=NumRighe;
               Riga:=0;
          end;
          {ora inserisco i dati}
          MyDataSourceList.DataSet.First;
          while not MyDataSourceList.DataSet.EOF do
          begin
               {disegno la singola riga}
               for Colonna:=0 to NumColonne-1 do
               begin
                    Self.Cells[Colonna,Riga]:=MyDataSourceList.DataSet.Fields[Colonna].AsString;
               end;
               Inc(Riga);
               MyDataSourceList.DataSet.Next;
          end;
     end;
     Self.AutoSizeColumns; //dimensiona le colonne correttamente
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;

procedure TExtDBListBox.SetDataSourceList(DataSourceList: TDataSource);
begin
     Self.MyDataSourceList:=DataSourceList;
end;

procedure TExtDBListBox.SetDataSourceIn(DataSourceIn: TDataSource);
begin
     Self.MyDataSourceIn:=DataSourceIn;
end;

procedure TExtDBListBox.SetDataSourceOut(DataSourceOut: TDataSource);
begin
     Self.MyDataSourceOut:=DataSourceOut;
end;

procedure TExtDBListBox.SetTitleColumns(TitleColumns: BOOLEAN);
begin
     Self.Intestazione:=TitleColumns;
end;

procedure TExtDBListBox.Click;
begin
     inherited Click;
     ShowMessage(Self.Cells[Self.Col,Self.Row]);
end;

{procedure TExtDBListBox.PrepareCanvas(aCol, aRow: integer; aState: TGridDrawState);
var
   a, b: integer;
begin
     a:=0;
     a:=b;
end;}

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

 

TinyPortal © 2005-2018