Solved

Setting event handlers for runtime created controls

Posted on 2001-08-09
8
220 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
 
LVL 2

Expert Comment

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

Never mind, it is ITugay who gave the answer

Rgds,
Frodo
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

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…
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…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

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

23 Experts available now in Live!

Get 1:1 Help Now