
本站地址:http://www.bajiao123.com

模板在DELPHI中的应用
在DELPHI 的 编 程 工 作 中 经 常 会 遇
到, 在 不 同 的 几 个 窗 体 中 拥 有 相 同 的 控 件, 事 件, 或
函 数。 分 别 创 建 或 修 改 它 们 将 是 一 件 非 常 乏 味 的 工
作。
利 用DELPHI 强 大 的 面 象 对 象 功 能, 问 题 迎 刃 而 解。
举 例 如 下: 在 窗 体BaseForm3 和BaseForm4 中 都 有 一 个 列 表 框ListBox1,
包 含 三 个 列 表 项
'the first item',
'the second item',
'the third item'
都 有 一 个 用 于 退 出 窗 体 的 按 钮btnExit, 及 一 个OnClose 事
件。
程 序 实 现 过 程 如 下:
首 先 新 建 一 个 项 目Poject1, 主 窗 体 为Form1;
然 后 创 建 一 个 用 于 模 板 的 窗 体BaseForm, 包 含ListBox1,btnExit,
及OnClose 事 件。
为ListBox1 添 加 三 个 选 项
'the first item',
'the second item',
'the third item'
btnExit 的OnClick 过 程 如 下
procedure TBaseForm.btnExitClick(Sender: TObject);
begin
Close;
end;
窗 体 的OnClose 过 程 如 下
procedure TBaseForm.FormClose(Sender:
TObject; var Action: TCloseAction);
begin
ShowMessage('The form will be closed!');
end;
然 后 选 择File 菜 单New 选 项 后, 将 打 开 一 个 包 含 所 用
窗 体 模 板 的 对 话 框, 选 择 其 中 的Project1 表 单 下 的BaseForm 模
板, 新 建 一 个 新 的 窗 体BaseForm3, 重 复 上 述 操 作, 创 建BaseForm4.
这 时BaseForm,BaseForm3,Form4 拥 有 一 样 的 外 观 及 属 性。 为 体 现Form2
和Form3 不 同 的 功 能, 在BaseForm3 中 加 一 个 按 钮btnShowMe, 双 击 按
钮btnShowMe 后 输 入 下 面 过 程
procedure TBaseForm3.btnShowMeClick(Sender: TObject);
begin
inherited;
ShowMessage('I am form2!');
end;
在BaseForm4 中 加 一 个 按 钮btnShowMe 及 一 个 编 辑 框Edit1,
双 击 按 钮btnShowMe 后 输 入 下 面 过 程
procedure TBaseForm4.btnShowMeClick(Sender: TObject);
begin
inherited;
edit.text:='I am form3!';
end;
在Form1 中 加 按 钮ShowForm2,ShowForm3 对 应OnClick 事 件 的 过 程 为
procedure TForm1.ShowBaseForm3Click(Sender: TObject);
begin
baseform3.show;
end;
procedure TForm1.ShowBaseForm4Click(Sender: TObject);
begin
baseform4.show;
end;
以 上 程 序 在DELPHI3 中 编 译 通 过。
本站地址:http://www.bajiao123.com

