質問

I am developing an Delphi 7 application with mdi childs forms in packages . There are some variables in main application ( such as Logined user , ... ) which i`d like to access them from packages forms . How can i access them ?

役に立ちましたか?

解決 2

The basic problem you have is that the child forms do not have any knowledge of the main form and so cannot access it. In fact, that's not really a problem in my view. That's good design and sound encapsulation. In an ideal world, the child forms should be able to be hosted anywhere.

So what you need is a way for the children to obtain information that their host provides. Since you don't want the children to know anything about the implementation of their host, this naturally leads towards using an interface.

So, decide what information the host needs to be able to supply to the children. For example:

type
  IAppProps = interface
    [...] // add a GUID here
    function GetUserName: string;
    property UserName: string read GetUserName;
    ... // more properties and/or methods as appropriate
  end;

You can implement this readily in your main form, or indeed some other class in your host application:

type
  TMainForm = class(TForm, IAppProps)
  ...
    function GetUserName: string;
  ...
  end;        

Next you need for your children to be able to receive an IAppProps interface reference.

type
  TBaseChildForm = class(TForm)
  private
    FAppProps: IAppProps;
  public
    property AppProps: IAppProps read FIAppProps write FIAppProps;
  end;

Then when your host creates a child form it simply writes:

ChildForm.AppProps := Self;

And it's all good.

他のヒント

The key in this question seems to be how to access defined interfaces.
Here SysUtils.Supports, which will return an interface if it's supported, might be the preferable way to go.

An example implementation could look like:

Declaration in the MainForm

type

  IMainFormProperties = interface
    ['{2F4913C6-09D6-472B-8D03-9A04B312B36C}']
    function GetAProperty: string;
    procedure SetAProperty(const Value:String);
    property AProperty: string read GetAProperty Write SetAProperty;
  end;


  TMainForm = class(TForm,IMainFormProperties)
     // ......
  private
    { Private-Deklarationen }
    FAProperty:String;
    function GetAProperty: string;
    procedure SetAProperty(const Value:String);

  public
    { Public-Deklarationen }
    property AProperty: string read GetAProperty Write SetAProperty;
  end;

And for the forms which shall access the MainForm

type
  IMainFormProperties = interface
    ['{2F4913C6-09D6-472B-8D03-9A04B312B36C}']
    function GetAProperty: string;
    procedure SetAProperty(const Value:String);
    property AProperty: string read GetAProperty write SetAProperty;
  end;


  TMDIChild = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public

  end;

var
  MDIChild: TMDIChild;

implementation

{$R *.dfm}

procedure TMDIChild.Button1Click(Sender: TObject);

var
  cnt : integer;
  iApp : IMainFormProperties;
begin
  begin
    if Supports(Application.Mainform, IMainFormProperties, iApp) then
      begin
        Showmessage(iApp.AProperty); // show existing value
        iApp.AProperty := Edit1.Text;// change existing value
      end;
  end;
end;

An related article can be found here: Interfaces in Delphi Programming 101

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top