Solved

Create components

Posted on 2002-03-20
9
143 Views
Last Modified: 2012-05-04
Hello!

I'm working on a project on which some (lots?) of you guys helped alot!!  I haven't done much Delphi in a long time but I managed to get the work done pretty much the past week and a half.

So here is the 7th question of the past days: How can I create components at runtime ?

Lets say I have an empty form with a button on top as the only component.  I want to be able to make textboxes appear when I click the button.

Any clue ??
0
Comment
Question by:qas
  • 5
  • 4
9 Comments
 
LVL 1

Expert Comment

by:slavikn
ID: 6883541
Hi,

1) In the var section of the form write:
   Edit1: TEdit;

2) On the button write:
   Edit1 := TEdit.Create(Self);
   With Edit1 Do Begin
      Parent := Form1;
      Height := 18;
      Width := 100;
      Left := 50;
      Top := 30;

      { Events and properties }
      OnClick := ....;
      OnMouseMove := ...;
      Cursor := crHandPoint;
   End;  



Don't forget to accept if this helps.   :-)
0
 

Author Comment

by:qas
ID: 6883625
Thanks alot!

This answer is almost it!

I'm now able to create new textboxes in the form but how do I reference to them since they are all named Edit1 ?  When I try to put some text in them (edit1.text := 'stuff';) only the last created is affected.

Also, can you get me some more precision on the EVENTS stuff ?  What goes after OnClick := ... ?  Isn't OnClick suppose to be an event ? Then why treat it like a property ??

Thanks again!
0
 

Author Comment

by:qas
ID: 6883626
Thanks alot!

This answer is almost it!

I'm now able to create new textboxes in the form but how do I reference to them since they are all named Edit1 ?  When I try to put some text in them (edit1.text := 'stuff';) only the last created is affected.

Also, can you get me some more precision on the EVENTS stuff ?  What goes after OnClick := ... ?  Isn't OnClick suppose to be an event ? Then why treat it like a property ??

Thanks again!
0
 

Author Comment

by:qas
ID: 6883632
Thanks alot!

This answer is almost it!

I'm now able to create new textboxes in the form but how do I reference to them since they are all named Edit1 ?  When I try to put some text in them (edit1.text := 'stuff';) only the last created is affected.

Also, can you get me some more precision on the EVENTS stuff ?  What goes after OnClick := ... ?  Isn't OnClick suppose to be an event ? Then why treat it like a property ??

Thanks again!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:slavikn
ID: 6883667
In 15 minutes I will post a full exmpale. Please wait a bit. Thanks.
0
 
LVL 1

Expert Comment

by:slavikn
ID: 6883669
In 15 minutes I will post a full exmpale. Please wait a bit. Thanks.
0
 
LVL 1

Expert Comment

by:slavikn
ID: 6883700
The answer is ready. Please reject my last answer (I clicked on it by mistake). Once you reject it, I will post the full code. Thanks.
0
 
LVL 1

Accepted Solution

by:
slavikn earned 200 total points
ID: 6883725
1) Create a new application.
2) Add 2 buttons.
3) This is the full code:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure MyEditsChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  MyEdits: Array[1..5] Of TEdit;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
   NumLoop: Integer;
begin
   For NumLoop := 1 To 5 Do Begin
      MyEdits[NumLoop] := TEdit.Create(Self);
      With MyEdits[NumLoop] Do Begin
         Parent := Form1;
         Height := 18;
         Width := 100;
         Left := 50;
         Top := 10 + 20 * NumLoop;

         { Events }
         OnChange := MyEditsChange;
      End;
   End;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
   NumLoop: Integer;
begin
   For NumLoop := 1 To 5 Do
      MyEdits[NumLoop].Text := 'Test';
end;

procedure TForm1.MyEditsChange(Sender: TObject);
begin
   Caption := (Sender As TEdit).Text;
end;

end.
0
 

Author Comment

by:qas
ID: 6883779
perfect!
thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to draw animated progressbar in Tvirtualstringtree 5 115
problem when i try to pack my dll file with upx 9 71
code issue 8 97
control image tags in a string ? 12 108
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now