Solved

create buttons during runtime

Posted on 2001-09-09
3
141 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
Comment Utility
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
Comment Utility
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
Comment Utility
thank you! mail arrived ;-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

16 Experts available now in Live!

Get 1:1 Help Now