Einzelnen Beitrag anzeigen

shmia

Registriert seit: 2. Mär 2004
5.508 Beiträge
 
Delphi 5 Professional
 
#4

Re: Zugriff auf Übergeordnetes Objekt?

  Alt 27. Okt 2005, 15:25
Zitat von Bloodfire:
MainForm (TForm) erstellt während der Laufzeit DlgForm (TForm), DlgForm soll nun Werte an MainForm zurückgeben, wie kann man das (am besten) umsetzen? (in der implementation die unit des MainForm-Objektes nochmal unter uses zu stellen, ist ja nicht sehr schön, bzw kann zu fehlern führen, nehm ich an?)
Das TDlgForm gibt seine Ergebnisse über Properties nach aussen.
Das Dialog-Formular wird so konstruiert, dass es ohne Änderung in eine andere Anwendung kopiert werden kann.
Das MainForm bindet die unit für DlgForm ein, nicht anderstrum.
Hier ein Beispiel für ein Dialog-Formular, dass einen Dateinamen abfrägt:
Delphi-Quellcode:
unit UInputQueryFile;

interface

uses
   Windows, SysUtils, Classes, Graphics, Controls, Forms,
   StdCtrls, Buttons, Mask, ToolEdit;

type
   TFrmInputQueryFile = class(TForm)
      FilenameEdit1: TFilenameEdit;
      BtnOk: TBitBtn;
      BtnCancel: TBitBtn;
    LblPromt: TLabel;
   private
    { Private-Deklarationen }
    function GetFilename: string;
    procedure SetFilename(const Value: string);
   public
    { Public-Deklarationen }
    property Filename:string read GetFilename write SetFilename;
   end;


function InputQueryFile(const ACaption, APrompt: string; var Value: string): Boolean;

implementation

{$R *.DFM}

function InputQueryFile(const ACaption, APrompt: string; var Value: string): Boolean;
var
   FrmInputQueryFile: TFrmInputQueryFile;
begin
   Result := False;
   FrmInputQueryFile := TFrmInputQueryFile.Create(nil);
   try
      FrmInputQueryFile.Caption := ACaption;
      FrmInputQueryFile.LblPromt.Caption := APrompt;
      FrmInputQueryFile.Filename := Value;

      if FrmInputQueryFile.ShowModal = mrOk then
      begin
         Value := FrmInputQueryFile.Filename;
         Result := True;
      end;
   finally
      FrmInputQueryFile.Free;
   end;
end;

{ TFrmInputQueryFile }

function TFrmInputQueryFile.GetFilename: string;
begin
   Result := FilenameEdit1.FileName;
end;

procedure TFrmInputQueryFile.SetFilename(const Value: string);
begin
   FilenameEdit1.FileName := Value;
end;
Andreas
  Mit Zitat antworten Zitat