AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Routine mit Namen aufrufen

Ein Thema von Sigi55 · begonnen am 20. Feb 2015 · letzter Beitrag vom 25. Feb 2015
 
BadenPower

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

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
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:26 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz