?
Solved

OnClick with TSpeedButton (created at runtime)

Posted on 1999-07-23
8
Medium Priority
?
486 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 

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] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses

601 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