Solved

Setting event handlers for runtime created controls

Posted on 2001-08-09
8
231 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

792 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