Solved

Create components

Posted on 2002-03-20
9
146 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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