Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create another component at runtime

Status
Not open for further replies.

DelphiAaron

Programmer
Jul 4, 2002
826
AU
i know how to create panels at runtime and set its properties.

but if i place a panel on the form and set its properties at design time.

how do i create more of that same panel at runtime ?

Aaron Taylor
John Mutch Electronics
 
Use the create method of Tpanel
TPanel.create(parent); the parameter can be the form on which the panel is created or it can be another component.
You can then set the name property. This sounds odd but you can create lots of identical panel (or whatever) in this way.

This example creates control panels for a motor control system and then adds some buttons and edit boxes onto them also positions them on screen.


Code:
procedure TMotorControl.initaliseMtr(Sender: Tobject; Count: Integer);

var ContPan: TPanel;
    Start,Stop,Reset,Mpars: TBitBtn;
    IGauge,Status,Fault: TEdit;
    oldmotor: TComponent;
  begin
  {Create motors as required}

  oldmotor := Findcomponent('Motor'+inttostr(count));
  oldmotor.free;

  Motor:= Tpanel.Create(MotorControl);
  with motor do
    begin
       name := 'Motor'+inttostr(count);
       Enabled := False;
       Parent := MotorControl;
       Width := 186;
       Height := 113;
       Top := 27 + Mx;
       Left := 10 + My;
       Caption := '';
    end;
  inc(My,198);
  if My > 600 then
     begin
       My := 0;
       inc(Mx,121);
     end;
  ContPan:=TPanel.Create(Motor);
  with contPan do
    begin
       Parent := motor;
       name := 'contpan' + inttostr(count);
       Caption := '';
       Height := 48;
       Width := 175;
       Top := 60;
       Left := 5;
    end;
  Start:=TBitBtn.Create(ContPan);
  with Start do
    begin
      Parent := ContPan;
      Height := 20;
      Width := 54;
      Top := 3;
      Left := 5;
   //   Glyph.loadfromfile(working + '\start.bmp');
      Buttons.GetBitmap(0,Glyph);
      Tag := count;
      OnClick := Start2Click;
    end;

  Stop := TBitBtn.Create(ContPan);
  with Stop do
     begin
       Parent := ContPan;
       Height := 20;
       Width := 54;
       Top := 3;
       Left := 60;
       Buttons.GetBitmap(1,Glyph);

   //    Glyph.loadfromfile(working +'\stop.bmp');
       Tag := count;
       OnClick := Stop2Click;
     end;
  Reset:=TBitBtn.Create(ContPan);
  with Reset do
     begin
       Parent := ContPan;
       Height := 20;
       Width := 54;
       Top := 3;
       Left := 115;
        Buttons.GetBitmap(2,Glyph);

     //  Glyph.loadfromfile(working +'\reset.bmp');
       Tag := count;
       OnClick := Reset2Click;
     end;
  Mpars:=TBitBtn.Create(Motor);
  Mpars.Parent := Motor;
  Mpars.Name := 'Mpars' + IntToStr(count);
  Mpars.Height := 54;
  Mpars.Width := 175;
  Mpars.Top := 5;
  Mpars.Left := 5;
  MotorImages.GetBitmap(1,  Mpars.Glyph);
  //Mpars.Glyph.loadfromfile(working +'\mtrstop.bmp');
  Mpars.Caption := 'Motor '+inttostr(count);
  Mpars.Tag := count;
  Mpars.OnClick := EditParametersClick;

  IGauge := Tedit.Create(ContPan);
  IGauge.Parent := ContPan;
  IGauge.Name := 'Igauge' + IntToStr(Count);
  IGauge.borderstyle := bsSingle;
  IGauge.Height := 17;
  IGauge.Width := 53;
  IGauge.Top := 25;
  IGauge.Left := 5;
  Igauge.autosize := false;

  IGauge.ReadOnly := True;
  IGauge.Font.name := 'courier';
  IGauge.Font.size := 10;

  Status := Tedit.Create(ContPan);
  Status.Parent := ContPan;
  Status.Name := 'Status' + IntToStr(Count);
  Status.Font.name := 'courier';
  Status.Font.size := 10;
  Status.Height := 17;
  Status.Width := 53;
  Status.Top := 25;
  Status.Left := 60;
  Status.ReadOnly := True;

  Fault := Tedit.Create(ContPan);
  Fault.Parent := ContPan;
  Fault.Name := 'Fault' + IntToStr(count);
  Fault.Font.name := 'courier';
  Fault.Font.size := 10;
  Fault.Height := 17;
  Fault.Width := 53;
  Fault.Top := 25;
  Fault.Left := 115;
  Fault.ReadOnly := True;

  end;

Steve: Delphi a feersum engin indeed.
 
Ah misread your post. Hmm not sure if you can do that,
Have a look at component templates, might do what you want if you save the panel as a template.
Yes I think this is the way to do it design your panel and select it. Then select 'create component template' from the Component menu. This creates a sort of instant component. that you can drop onto forms. To be honest I haven't tried to 'create' one of these on the fly, but the help file seems to suggest that you should be able to do this.


You could make it into a full component (even more work to set this up).

But there is only a lot of code in my example because it sets up a lot of stuff, It only gets called once for each new instance of the panel. So another way of looking at it is that it makes multiple copies.





Steve: Delphi a feersum engin indeed.
 
i found this but couldnt get it to work

3 steps for dublicate component at runtime.
First is copy the component in clipboard.
Second is rename component which is still on the form.
And the last step is get component from clipboard.
Don't forget to register your component's class.

uses Clipbrd;
...
procedure TForm1.Button1Click(Sender: TObject);
begin
Clipboard.SetComponent(Button1);
Button1.Name:='OriginalButton';
Clipboard.GetComponent(Self,GroupBox1);
end;
...
initialization
RegisterClasses([TButton]);
...



Aaron Taylor
John Mutch Electronics
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top