?
Solved

Setting event handlers for runtime created controls

Posted on 2001-08-09
8
Medium Priority
?
243 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
[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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 9

Accepted Solution

by:
ITugay earned 600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

743 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