Einzelnen Beitrag anzeigen

BadenPower

Registriert seit: 17. Jun 2009
616 Beiträge
 
#17

AW: Routine mit Namen aufrufen

  Alt 21. Feb 2015, 12:31
Oder Du machst aus der anderen Unit einfach eine "Form-Unit", welche Du nicht einmal anzeigen lassen musst.


kleines Copy-und-Paste Beispiel

MainForm
Delphi-Quellcode:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure Job1();
    procedure Job2();
    procedure Job3();
    procedure Button1Click(Sender: TObject);
  private
    procedure ExecuteJobs();
    procedure ExecuteRoutine(AInstance: TObject; AName: string);
  public
    Memo1: TMemo;
    Button1: TButton;
    ListBox1: TListBox;
    constructor Create(AOwner: TComponent); override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  TExecute = procedure of object;

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited;
  Position := poScreenCenter;
  Height := 500;
  Width := 600;
  Memo1 := TMemo.Create(Self);
  Memo1.Parent := Self;
  Memo1.Left := 300;
  Memo1.Top := 10;
  Memo1.Width := 280;
  Memo1.Height := 400;
  Button1 := TButton.Create(Self);
  Button1.Parent := Self;
  Button1.Left := 100;
  Button1.Top := 420;
  Button1.Width := 100;
  Button1.Height := 25;
  Button1.Caption := 'Jobs starten';
  Button1.OnClick := Button1Click;
  ListBox1 := TListBox.Create(Self);
  ListBox1.Parent := Self;
  ListBox1.Left := 10;
  ListBox1.Top := 10;
  ListBox1.Width := 280;
  ListBox1.Height := 400;
  ListBox1.Items.Add('Form1/Job1');
  ListBox1.Items.Add('Form1/Job2');
  ListBox1.Items.Add('Form1/Job3');
  ListBox1.Items.Add('Form2/Job1');
  ListBox1.Items.Add('Form2/Job2');
  ListBox1.Items.Add('Form2/Job3');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ExecuteJobs();
end;

procedure TForm1.ExecuteJobs();
var
  liZ1: Integer;
  lObjectName: String;
  lProcedureName: String;
  lPos: Integer;
begin

  for liZ1 := 0 to ListBox1.Count - 1 do
   begin
    if ListBox1.Items[liZ1] <> 'then
     begin
      lObjectName := '';
      lProcedureName := '';
      lPos := Pos('/', ListBox1.Items[liZ1]);
      lObjectName := Copy(ListBox1.Items[liZ1], 1, lPos - 1);
      lProcedureName := Copy(ListBox1.Items[liZ1], lPos + 1, MaxInt);
      if ((lObjectName <> '') and (lProcedureName <> '')) then
       begin
         Memo1.Lines.Add('Methode ' + lProcedureName + ' in ' + lObjectName + ' starten.');
         ExecuteRoutine(Application.FindComponent(lObjectName), lProcedureName);
         Memo1.Lines.Add('Methode ' + lProcedureName + ' in ' + lObjectName + ' beendet.');
       end;
     end;
   end;

end;

procedure TForm1.ExecuteRoutine(AInstance: TObject; AName: string);
var
  lRoutine: TMethod;
  lExecute: TExecute;
begin
  lRoutine.Data := Pointer(AInstance);
  lRoutine.Code := AInstance.MethodAddress(AName);
  if (lRoutine.Code = nil) then Exit;
  lExecute := TExecute(lRoutine);
  lExecute;
end;

procedure TForm1.Job1();
begin
  ShowMessage('Form1 - Job1');
end;

procedure TForm1.Job2();
begin
  ShowMessage('Form1 - Job2');
end;

procedure TForm1.Job3();
begin
  ShowMessage('Form1 - Job3');
end;

end.
Unit2
Delphi-Quellcode:
unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    procedure Job1();
    procedure Job2();
    procedure Job3();
  private
  public
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Job1();
begin
  ShowMessage('Form2 - Job1');
end;

procedure TForm2.Job2();
begin
  ShowMessage('Form2 - Job2');
end;

procedure TForm2.Job3();
begin
  ShowMessage('Form2 - Job3');
end;

end.
Programmieren ist die Kunst aus Nullen und Einsen etwas sinnvollen zu gestalten.
Der bessere Künstler ist allerdings der Anwender, denn dieser findet Fehler, welche sich der Programmierer nicht vorstellen konnte.
  Mit Zitat antworten Zitat