Solved

Setting event handlers for runtime created controls

Posted on 2001-08-09
8
226 Views
Last Modified: 2010-04-06
Hi, apologies as I've actually asked this one before but completely forgot the answer!  I want to create multiplt controls at runtime (eg Tpanels) and set the OnClick events to a single event handler that can easily distinguish which particular control has been clicked. Please can someone let me know the exact syntax I need please.  many thanks, P.
0
Comment
Question by:Pandora
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 9

Accepted Solution

by:
ITugay earned 150 total points
ID: 6368478
Hi Pandora,

here is it:

type
  TForm1 = class(TForm)
    SpeedButton1: TSpeedButton;
    procedure FormClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormClick(Sender: TObject);
begin
  beep;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  I: Integer;
  P: TPanel;
begin
  for I := 0 to 9 do
  begin
    P := TPanel.Create(Self);
    P.ALign := alTop;
    P.Parent := Self;
    // P.Tag := I;
    P.OnClick := FormClick;
  end;
end;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6368503
Igor is right. And "Sender" then contains the panel, that was clicked.

Regards, Madshi.
0
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6368512
And distinguish which particular control has been clicked, in the event handler (FormClick in the example) use Sender. If you want to work with it as it is TPanel use (Sender as TPanel):
  (Sender as TPanel).Caption := 'blahblah';

To be sure it is TPanel write:
if Sender is TPanel then
  (Sender as TPanel).Caption := 'blahblah';

Rgds,
Frodo

0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6368525
Hehe. Madshi, collision again? :))

Never mind, it is ITugay who gave the answer

Rgds,
Frodo
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6368551
Yep...   (-:
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6368563
hi all :-)
0
 
LVL 17

Expert Comment

by:inthe
ID: 6368626
hello,
""apologies as I've actually asked this one before but completely forgot the answer""  

just to mention that if you forget your answers you can view it in your history (by clicking on your username and viewing history).

i am guessing this is your old question you meant :

http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20096042

   :-)
0
 
LVL 1

Author Comment

by:Pandora
ID: 6369565
Hee hee - thank you all!!  Fantastic answers all round and all the nicer for the collaboration.  I knew it was somthing like that!

InThe, Your comment really made me giggle, and you put me to shame with your memory and organisational skills!  Its a bit bizarre, but that question has completely disappeared from my question history, although I've reported it as an EE bug, I thought I'd lost it forever - thanks for finding it for me!

Best wishes all, and once again many thanks, P :)
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 152
Delphi : could not find program, '...exe' 2 171
Delphi: Connect to running MS Outlook 4 86
How to Get Images From Server using App Tethering 11 29
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

772 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