了解德尔福通知

时间:2016-05-31 12:42:55

标签: forms delphi notify

今天我正在尝试了解通知。 我有一个这样的主要形式:

Application main form

在表单中,如您所见,我使用了一个LMDFormDisplay组件用于mbed表单。 当用户单击位于表单左上角的图标时,我会显示名为frmList的第二个表单。 以下是主要表单的代码:

unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.ExtCtrls, LMDControl, LMDCustomControl, LMDCustomPanel,
  LMDCustomBevelPanel, LMDFormDisplay, dxGDIPlusClasses, cySkinButton;

type
  TfrmMain = class(TForm)
    LMDFormDisplay: TLMDFormDisplay;
    pnlCmd: TPanel;
    btnShowListForm: TcySkinButton;
    procedure btnShowListFormClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

uses list;

procedure TfrmMain.btnShowListFormClick(Sender: TObject);
begin
  LMDFormDisplay.Form := frmList;
  frmMain.Caption := 'Form embed - ' + frmList.Caption;
end;

end.

List表单如下所示:

List form

这是列表形式的代码:

unit list;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TfrmList = class(TForm)
    btnCloseListForm: TButton;
    txtListForm: TStaticText;
    btnShowDetailForm: TButton;
    procedure btnCloseListFormClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmList: TfrmList;

implementation

{$R *.dfm}

procedure TfrmList.btnCloseListFormClick(Sender: TObject);
begin
  Self.Close;
end;

end.

最后,我有一个名为frmDetail的第三个表单,如下所示:

Detail form 这里有代码:

unit detail;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TfrmDetail = class(TForm)
    txtDetail: TStaticText;
    btnCloseDetailForm: TButton;
    procedure btnCloseDetailFormClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmDetail: TfrmDetail;

implementation

{$R *.dfm}

procedure TfrmDetail.btnCloseDetailFormClick(Sender: TObject);
begin
  Self.Close;
end;

end.

我想通过点击列表表单中的“显示详细信息表单”按钮来显示详细信息表单。我不想在列表表单代码中添加详细信息表单单元,而是向主表单发送通知,例如:“嘿列表表单已通知打开详细信息表单”。 我知道我可以使用消息,但是,如果可能的话,我不想使用它们,而是使用某种通知。 问题是我不知道如何使用它们。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

我的评论代码看起来像这样:

  TfrmMain = class(TForm)
    LMDFormDisplay: TLMDFormDisplay;
    pnlCmd: TPanel;
    btnShowListForm: TcySkinButton;
    procedure btnShowListFormClick(Sender: TObject);
  private
    { Private declarations }
  public
    procedure ShowDetailForm(Sender: TObject);
  end;

procedure TfrmMain.btnShowListFormClick(Sender: TObject);
begin
  LMDFormDisplay.Form := frmList;
  frmList.btnShowDetailForm.OnClick := ShowDetailForm;
  frmMain.Caption := 'Form embed - ' + frmList.Caption;
end;

procedure TfrmMain.ShowDetailForm(Sender: TObject);
begin
  LMDFormDisplay.Form := frmDetail; // or other 'show' code
end;

注意:我不一定在实际应用程序中执行此操作,但它应该用于学习如何在运行时分配事件,并允许您拥有一个不知道的事件的ListForm详细表格。

您甚至可以更进一步,在运行时使用一系列TNotifyEvents和Captions并在ListForm上创建按钮,而不是使用表单设计器。

相关问题