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
Solved

create buttons during runtime

Posted on 2001-09-09
3
145 Views
Last Modified: 2010-04-06
hi! i want to create some buttons (the count of the buttons is unknown) during runtime. when you klick them an shellexecute command shall be executed with a button specific file. how can i do that ?
0
Comment
Question by:ItsMe
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
lucika earned 100 total points
ID: 6468414
unit Unit1;

interface

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

type
  PListButtonRec=^TListButtonRec;
  TListButtonRec=record
    tag: integer;
    Button: TButton;
    MyCommand: String;
  end;

  TForm1 = class(TForm)
    AddButton: TButton;
    Edit1: TEdit;
    DelButton: TButton;
    procedure AddButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure MyButtonClick(Sender: TObject);
    procedure DelButtonClick(Sender: TObject);

  private
    ListButton: TList;
    MaxTag: integer;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.MyButtonClick(Sender: TObject);
var i: integer;
begin
  for i:=0 to ListButton.Count-1 do
    if PListButtonRec(ListButton.Items[i])^.tag=(Sender as TWinControl).Tag then
    begin
      ShowMessage(PListButtonRec(ListButton.Items[i])^.MyCommand);
      exit;
    end;
end;

procedure TForm1.AddButtonClick(Sender: TObject);
var
  tmButton: PListButtonRec;
begin
  new(tmButton);
  inc(MaxTag);

  tmButton^.tag:=MaxTag;
  tmButton^.MyCommand:='Run command ('+inttostr(MAxTag)+')';

  tmButton^.Button:=TButton.Create(self);
  tmButton^.Button.Parent:=self;
  tmButton^.Button.Left:=MaxTag*100;
  tmButton^.Button.Caption:='#'+inttostr(MAxTag);
  tmButton^.Button.Tag:=MaxTag;
  tmButton^.Button.OnClick:=MyButtonClick;

  ListButton.Add(tmButton);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListButton:=TList.Create;
  Maxtag:=0;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var i: integer;
begin
  for i:=0 to ListButton.Count-1 do
    Dispose(PListButtonRec(ListButton.Items[i]));
  ListButton.Destroy;
end;


procedure TForm1.DelButtonClick(Sender: TObject);
var i: integer;
    tmButton: PListButtonRec;
    mtag: integer;
begin
  try
  mtag:=StrToInt(Edit1.text);
  for i:=0 to ListButton.Count-1 do
    if PListButtonRec(ListButton.Items[i])^.tag=mTag then
    begin
      tmButton:=PListButtonRec(ListButton.Items[i]);
      tmButton^.Button.Destroy;
      Dispose(tmButton);
      ListButton.Delete(i);
      exit;
    end;
  except
      showmessage('Error');
  end;
end;

end.
0
 

Author Comment

by:ItsMe
ID: 6468482
hi lucika!
i don't get it to work within my app (The declaration of myButtonClick fails). please send me the whole project file to philipl@web.de

thanks a lot !

kind regards
ItsMe
0
 

Author Comment

by:ItsMe
ID: 6468537
thank you! mail arrived ;-)
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

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 238
Best Firemonkey component pack 1 119
Downloading email attachments 2 79
TEMBEDDEDWB how can i change its user agent ? 8 69
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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