[fpc-pascal] How do I add a TComponent to TPanel dynamically?

Danie dawessels at telkomsa.net
Tue Aug 26 00:10:15 CEST 2008


I hope this the right forum for this question (gtk on
lazarus-0.9.24-1.fc8 rpm on fpc-2.2.0-10.fc8 rpm)(but I know there are
people here that can help me %^).  
I would like to add a special TShape (descendant or TComponent) to a
panel.
I tried the following but must be missing something.

type
  pControlVolume = ^TControlVolume ; {Forward}
  TControlVolume = class(TComponent {like a TShape})
  private    { private declarations }
    fNumber : integer ;
    fCVName : string ;
    fCVShape : tShape ;
    fPrevious : pControlVolume ;
    fNext  : pControlVolume ;
  public    { public declarations }
   property Number : integer Read fNumber  Write  fNumber ;
   property CVName : string  Read fCVName  Write  fCVName ;
   property CVShape : tShape Read fCVShape Write  fCVShape;
   property Previous: pControlVolume Read fPrevious Write fPrevious ;
   property Next    : pControlVolume Read fNext     Write fNext     ;
    Constructor Create( Sender: TComponent ) {Override} ;
    Destructor Destroy {Override} ;
    Procedure Init();
    Procedure Done();
     end;
:
: 
implementation

 Constructor TControlVolume.Create( Sender:TComponent );
 begin
   Inherited Create(Sender);
   fCVShape.Create( Sender );
 end;
 
 Destructor TControlVolume.Destroy;
 begin
   fCVShape.Destroy;
   Inherited Destroy ;
 end;

 Procedure TControlVolume.Init();
 begin    {no Inherited TComponent.Init ;}
   fCVName := 'NoNameYet' ;
 end;
 
 Procedure TControlVolume.Done();
 begin   {no Inherited Done ;}
   fCVName := '';
 end;
:
:
{then in the Form's Button to add a new CV...}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if pTopCV = nil
    then  begin
      NEW( pTopCV );
      pTopCV^ := TControlVolume.Create(PanelDraft);  
      pLastCV := pTopCV ;
      pLastCV^.Previous := nil ;
      pLastCV^.Number := 1 ;
      end
    else begin
      NEW(pLastCV^.Next );
      pLastCV^.Next^ := TControlVolume.Create(PanelDraft);
      pLastCV^.Next^.Previous := pLastCV ;
      pLastCV^.Next^.Number := pLastCV^.Number + 1 ;
      pLastCV := pLastCV^.Next ;
      end ;
  pLastCV^.CVName:='CV-' + IntToStr(pLastCV^.Number) ;
  pLastCV^.Next := nil ;
  pLastCV^.CVShape := TShape.Create( PanelDraft ) ;
  pLastCV^.CVShape.Parent := PanelDraft ;
  pLastCV^.CVShape.Left := 10 ;
  PanelDraft.Paint ;
end;
:
:
Compiling gives:
unit1.pas(29,17) Warning: An inherited method is hidden by "constructor
TControlVolume.Create(TComponent)"
unit1.pas(30,16) Warning: An inherited method is hidden by "destructor
TControlVolume.Destroy"
unit1.pas(197,19) Warning: use extended syntax of NEW and DISPOSE for
instances of objects
unit1.pas(205,25) Warning: use extended syntax of NEW and DISPOSE for
instances of objects
Project "cvproject" successfully built. :)

Is the principal right because I get an error on adding a new
TControleVolume? External SEGSEGV crash.
It seems like I am not defining the constructor properly???

I thought of using a stringlist for handling (--keeping record of) the
string of objects (shapes with names and real space position properties)
but am a bit lost with how to address the objects through the
stringlist. All advice and pointers are welcome.

TIA
DanieW




More information about the fpc-pascal mailing list