Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create components

Posted on 2002-03-20
9
Medium Priority
?
149 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
Independent Software Vendors: 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 800 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

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

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…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

670 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