?
Solved

Delphi Add Components at runtime on buttonclick

Posted on 2011-05-03
2
Medium Priority
?
326 Views
Last Modified: 2012-06-27
Hi all,

I need to do the following:

On a buttonclick procedure...

I have a panel with 2 memos, 1 checkbox and 1 dropdown.

I need a way to add a new intance of the whole panel, with it's components.

Adding one after the other.

Is this possible?

THanks
0
Comment
Question by:error77
2 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 35518084
You can try this
unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm3 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Panel1: TPanel;
    Memo1: TMemo;
    Memo2: TMemo;
    CheckBox1: TCheckBox;
    ComboBox1: TComboBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}


procedure TForm3.Button1Click(Sender: TObject);
var
  NewPanel: TPanel;
  I: Integer;
  Ctrl: TControl;
  NewCtrl: TControl;
  CompClass: TControlClass;
begin
  NewPanel := TPanel.Create(self);
  NewPanel.Parent := Self;
  NewPanel.Top := Panel1.Top + Panel1.Height + 5;
  NewPanel.Left := Panel1.Left;
  NewPanel.Height := Panel1.Height;
  NewPanel.Width := Panel1.Width;

  for I := 0 to Panel1.ControlCount - 1 do
  begin
    Ctrl := Panel1.Controls[I];

    CompClass := TControlClass(FindClass(Ctrl.ClassName));
    NewCtrl := CompClass.Create(Self) as TControl;
    TControl(NewCtrl).Parent := NewPanel;
    TControl(NewCtrl).Top := Ctrl.Top;
    TControl(NewCtrl).Left := Ctrl.Left;
    TControl(NewCtrl).Width := Ctrl.Width;
    TControl(NewCtrl).Height := Ctrl.Height;
  end;

end;

initialization
   RegisterClass(TMemo);
   RegisterClass(TCheckBox);
   RegisterClass(TComboBox);

end.

Open in new window

0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35682843
this may be advanced atm ... but consider using frames
you design a frame with all you want on it

the next you do is create an instance of the frame and add it to form

i dunno if you have ever used frames ?
http://etutorials.org/Programming/mastering+delphi+7/Part+II+Delphi+Object-Oriented+Architectures/Chapter+8+The+Architecture+of+Delphi+Applications/Understanding+Frames/
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question