Solved

create buttons during runtime

Posted on 2001-09-09
3
142 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 2010 Export to pdf 2 285
PDF library for Delphi 2 104
Delphi Dbf export problem to a Visual Foxpro application 6 160
control image tags in a string ? 12 110
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…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now