Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-08

How do I add a TComponent to TPanel dynamically?

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Danie

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

Marco van de Voort
to Danie

In our previous episode, Danie said:

Show quoted text 6 lines
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.

I only looked quickly, but it seems you create the tshape with the parent of the controlvolume class as owner (..create(sender)), but you destroy it manually. Don't.

Mattias Gärtner
to Marco van de Voort

Zitat von Marco van de Voort :

Show quoted text 11 lines
In our previous episode, Danie said: > 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. I only looked quickly, but it seems you create the tshape with the parent of the controlvolume class as owner (..create(sender)), but you destroy it manually. Don't.

Why not?

Mattias

Danie
to Stephano

Great Stephano!
That did the job.

Stephano wrote
fCVShape.Create(Sender) -> fCVShape := TShape.Create(Sender) fCVShape.Destroy -> fCVShape.Free

Thanks
DanieW

Michael Green
to Danie: I hope this the right forum for this question (gtk on lazarus-0.9.24-1.fc8 rpm on…

Hi Danie,

Other than what was pointed out with create/destroy, wouldn't it create a memory leak with the NEW?

Show quoted text 7 lines
procedure TForm1.Button1Click(Sender: TObject); begin if pTopCV = nil then begin NEW( pTopCV ); pTopCV^ := TControlVolume.Create(PanelDraft);

Instead, simply do:

  if not assigned(pTopCV) then begin
    pTopCV^ := TControlVolume.Create(Paneldraft);

(Same goes for the pTopCV.next entry).

-- Mike

Danie
to Michael Green

Hi Micheal

Michael Green wrote 16 lines
Hi Danie, Other than what was pointed out with create/destroy, wouldn't it create a memory leak with the NEW? > procedure TForm1.Button1Click(Sender: TObject); > begin > if pTopCV = nil > then begin > NEW( pTopCV ); > pTopCV^ := TControlVolume.Create(PanelDraft); Instead, simply do: if not assigned(pTopCV) then begin pTopCV^ := TControlVolume.Create(Paneldraft); (Same goes for the pTopCV.next entry). -- Mike

I really do not know (but would like to) but it also does not work. - crashes at the creation point. Can you get it to work? DanieW