?
Solved

Adding an event to a Button created at runtime

Posted on 2009-12-27
5
Medium Priority
?
233 Views
Last Modified: 2012-05-08
Hi all,

I am creating a close button at runtime. Create the button works fine but I now need to add an onclick event to when this button is clicked on, so I can close PageControl Active Pages. I just cannot get it to work.

How do I do this please?

Here is the current code I'm using below:


Button := TSpeedButton.Create(PageControl1);
  Button.Parent := PageControl1;
  Button.Top := 0;
  Button.Left := PageControl1.Width - Button.Width;
  Button.Caption:=('X');
 // Button.OnClick := <-- This doesn't work
  Button.Visible := True;

Open in new window

0
Comment
Question by:ST3VO
  • 2
  • 2
5 Comments
 
LVL 31

Accepted Solution

by:
moorhouselondon earned 1000 total points
ID: 26126680
You create a procedure called:-

procedure ButtonClick(Sender: TObject);
begin
...
end;

and line 6 of your code would then read:-

Button.OnClick :=ButtonClick;
0
 
LVL 23

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 1000 total points
ID: 26126690
Basically you can assign procedures in this way:

Crate a procedure in your private section like

private
    { Private declarations }
    procedure MyCloseButton(Sender: TObject);

then the procedure will be, for example

procedure TForm1.MyCloseButton(Sender: TObject);
begin
   Close; //or your code to close pagecontrol activepage
end;

Now, in your button construction simply assign the procedure

Button := TSpeedButton.Create(PageControl1);
  Button.Parent := PageControl1;
  Button.Top := 0;
  Button.Left := PageControl1.Width - Button.Width;
  Button.Caption:=('X');
  Button.OnClick := MyCloseButton;<-- This assign your close precedure
  Button.Visible := True;
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26126692
Was writing during the moorhouselondon posting, so didn't see his answer.
Don't consider mine as answer
0
 

Author Closing Comment

by:ST3VO
ID: 31670166
Thanks!
0
 

Author Comment

by:ST3VO
ID: 26126703
I had to share both as answers because although you both said the same thing, I had something else missing in my code which was added by Ferruccio68, so I've used both codes to get this working.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

829 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