Solved

Create components

Posted on 2002-03-20
9
142 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

17 Experts available now in Live!

Get 1:1 Help Now