要实现子窗体与父窗体之间的通讯,有多种方法(比如:重载子窗体的构造函数,将父窗体的引用作为参数传递给子窗体)。下面我要介绍的是利用自定义事件的方法,它能够最大程度的避免模块之间的耦合,充分体现面向对象的优点。
先将效果图展示给大家:
[[The No.1 Picture.]]
下面是实现代码:
Unit2 //子窗体
type
//声明自定义事件的类型(类似于C#中的委托)
TMyEventHandle = procedure (Sender: TObject; Content: string) of object;
TForm2 = class(TForm)
ComboBox1: TComboBox;
Label1: TLabel;
procedure ComboBox1Click(Sender: TObject);
private
FOnSelectionChanged: TMyEventHandle;
public
//声明自定义事件
property OnSelectionChanged: TMyEventHandle
read FOnSelectionChanged write FOnSelectionChanged;
end;
procedure TForm2.ComboBox1Click(Sender: TObject);
begin
//当选择改变时,触发自定义的事件
if Assigned(FOnSelectionChanged) then
先将效果图展示给大家:
下面是实现代码:
Unit2 //子窗体
type
//声明自定义事件的类型(类似于C#中的委托)
TMyEventHandle = procedure (Sender: TObject; Content: string) of object;
TForm2 = class(TForm)
ComboBox1: TComboBox;
Label1: TLabel;
procedure ComboBox1Click(Sender: TObject);
private
FOnSelectionChanged: TMyEventHandle;
public
//声明自定义事件
property OnSelectionChanged: TMyEventHandle
read FOnSelectionChanged write FOnSelectionChanged;
end;
procedure TForm2.ComboBox1Click(Sender: TObject);
begin
//当选择改变时,触发自定义的事件
if Assigned(FOnSelectionChanged) then