2004. október 8., péntek

Make a form use two frames in separate units


Problem/Question/Abstract:

How can I make a function which will create a frame (given its class) in a new form?

Answer:

Maybe you should use an enumerated value which is an index of an array of your frame classes. An example with a form using two frames in seperate units:

unit UnitTestForm;

interface

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

type
  {'class of' and enumeration}
  TFrameClass = class of TFrame;
  TFrameEnm = (frm_one, frm_two);

  {array of classes}
  TFrameClasses = array[TFrameEnm] of TFrameClass;

  {test form}
  TForm1 = class(TForm)
    btnCreateFrame1: TButton;
    btnCreateFrame2: TButton;
    procedure btnCreateFrame1Click(Sender: TObject);
    procedure btnCreateFrame2Click(Sender: TObject);
  private
    function CreateFrame(FrameEnm: TFrameEnm): TForm;
  public
  end;

var
  Frames: TFrameClasses = (TFrameOne, TFrameTwo);

  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnCreateFrame1Click(Sender: TObject);
var
  Form: TForm;
begin
  Form := Self.CreateFrame(frm_one);
  Form.ShowModal;
end;

procedure TForm1.btnCreateFrame2Click(Sender: TObject);
var
  Form: TForm;
begin
  Form := Self.CreateFrame(frm_two);
  Form.ShowModal;
end;

function TForm1.CreateFrame(FrameEnm: TFrameEnm): TForm;
var
  aFrame: TFrame;
begin
  Result := TForm.Create(nil);
  aFrame := Frames[FrameEnm].Create(Result);
  aFrame.Parent := Result;
  aFrame.Align := alClient;
end;

Nincsenek megjegyzések:

Megjegyzés küldése