Solved

Create components

Posted on 2002-03-20
9
144 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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