?
Solved

OnClick with TSpeedButton (created at runtime)

Posted on 1999-07-23
8
Medium Priority
?
481 Views
Last Modified: 2012-05-04
I have created at runtime a array[1..10] of TSpeedButton.  This is done in the OnCreate-event of the form:

  for dummy:=1 to 10 do
    begin
      BtnProg[dummy]:=TSpeedButton.create(pnlTaakbalk);
      BtnProg[dummy].parent:=pnlTaakbalk;
      BtnProg[dummy].caption:='BtnProg'+inttostr(dummy);
      BtnProg[dummy].down:=false;
      BtnProg[dummy].groupindex:=1;
      BtnProg[dummy].height:=22;
      BtnProg[dummy].left:=0;
      BtnProg[dummy].margin:=4;
      BtnProg[dummy].tag:=0;
      BtnProg[dummy].top:=3;
      BtnProg[dummy].visible:=false;
      BtnProg[dummy].width:=72;
    end;

The SpeedButtons will become visible en placed at the correct place when the user activate something in the program.  I have test it and it works fine.

Now I want to create an OnClick-event that runs everytime someone hit any of the runtime-SpeedButtons.

But at design-time the buttons are not there.  How can I create an OnClick event and assign it to BtnProg[1]...?

Is it possible to make one OnClick-event and assign it to all BtnProg[1] to BtnProg[10]?

I hope you understand what I mean.

PS Sorry for my poor English.
0
Comment
Question by:ka1a
[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
  • 4
  • 3
8 Comments
 
LVL 1

Expert Comment

by:m_adil
ID: 1390724
Yes you can do this.
Create a procedure say ClickMe and assign this procedure to all of the buttons like this

BtnProg[dummy]OnClick = ClickMe;
Also add this line to assign the tag to the buttons
BtnProg[dummy].Tag:=dummy;

if you want to do different tasks with these buttons then you can use (Sender as TSpeedButton).Tag to find which button is clicked.

Hope this will help
0
 

Author Comment

by:ka1a
ID: 1390725
Thanks m_adil.

Is it possible to let me now:

1) where exactly i have to assign the ClickMe-procedure to all the BtnProg[dummy] Speedbuttons.  Do I have to do this in the OnCreate-event of the form?

2) How exactly can I now with BtnProg[?] was hit by using the TAG-property?  You use sender?  Can you give an short example of the correct use in an if..then statement.

Thanks.

Are the 30 points enough?  If not, let me now.

Thanks again.

Dirk.
0
 
LVL 1

Expert Comment

by:m_adil
ID: 1390726
Hi, give me some time.
I create an example for you.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ka1a
ID: 1390727
Thank you.
0
 
LVL 2

Expert Comment

by:CalvinDay
ID: 1390728
Assign the ClickMe procedure right after:
BtnProg[dummy].width:=72;

procedure ClickMe(Sender:TObject);
begin
case (Sender as TSpeedButton).Tag of
  1:DoSomething;
  2:DoSomethingElse;
  end;
end;
0
 
LVL 1

Expert Comment

by:m_adil
ID: 1390729
Hi sorry for the delay. My proxy server is a hell.
Here is the example. Hope this will help.

//**************************************************
unit ex;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
    procedure ClickMe(Sender: TObject);
  private
    { Private declarations }
    nbtn:integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ClickMe(Sender: TObject);
var
   i:integer;
begin
i:=(Sender as TButton).Tag;
if i=0 then
   showmessage('0')
else if i=1 then
   showmessage('1');
// and so on...
end;

procedure TForm1.Button2Click(Sender: TObject);
var
 newbtn: array [0..9] of Tbutton;
 i:integer;
begin
for i:=0 to 9 do
begin
 newbtn[i]:=Tbutton.create(self);
 newbtn[i].parent:=self;
 newbtn[i].name:='Command'+inttostr(i);
 newbtn[i].caption:='Command '+inttostr(i);
 newbtn[i].setbounds(i*138,168,75,25);
 newbtn[i].dragcursor:=crDefault;
 newbtn[i].onClick:=ClickMe;
 newbtn[i].tag:=i;
end;

end;

end.

0
 

Author Comment

by:ka1a
ID: 1390730
Thanks m_adil.

How can I get the prommised points to you ?
0
 
LVL 1

Accepted Solution

by:
m_adil earned 120 total points
ID: 1390731
So I think I mark this comment as answer so that you grade the points.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

719 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