whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

91 Visitors Online


 
...add a procedural type to TList?
Autor: César Nicolás Peña Núñez
[ Print tip ]  

Tip Rating (4):  
     


unit Unit1;

interface

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

type
  
TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    
{ Private declarations }
  
public
    
{ Public declarations }
  
end;

var
  
Form1: TForm1;

implementation

{$R *.DFM}

type
  
TEventObject = class
  protected
    
FEvent: TNotifyEvent;
  published
    property 
Event: TNotifyEvent read FEvent write FEvent;
  end;

  // Zur Liste hinzufügen
  // To add to the list
procedure TForm1.Button1Click(Sender: TObject);
var 
  
A: TEventObject;
begin
  
A       := TEventObject.Create;
  A.Event := Button1Click; // or any TNotifyEvent
  
ListBox1.Items.AddObject('Button1Click', A);
end;

// Den Event aufrufen
// To call the event
procedure TForm1.ListBox1Click(Sender: TObject);
begin
  if 
ListBox1.ItemIndex <> -1 then
    
TEventObject(ListBox1.Items.Objects[ListBox1.ItemIndex]).Event(Self);
end;

end.



// 2. Lösung
// 2nd solution

{....}
  
private
    
FList: TList;
  end;
{....}


procedure TForm1.FormCreate(Sender: TObject);
begin
  
FList := TList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  
FList.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  
ShowMessage('event');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  
P: TNotifyEvent;
begin
  
// Get pointer to event
  // Pointer des Events holen
  
P := Button1Click;
  // Add Pointer to List
  // Pointer zur Liste hinzufügen
  
FList.Add(@P);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  
P: TNotifyEvent;
begin  // Get first event from list
  // Den ersten Event aus der Liste holen
    
@P := FList[0];
  // Execute event
  // Event ausführen
  
P(Sender);
end;



 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners