gpt4 book ai didi

delphi - 如何在 Delphi 中将 TPageControl 选项卡分解为单独的文件?

转载 作者:行者123 更新时间:2023-12-03 14:50:47 25 4
gpt4 key购买 nike

我有一个 VCL 表单,上面有五个选项卡 TPageControl。每个 TTabSheet 的内容都相当不相关,并且有很多单独的控件和处理逻辑...所以我想将其分解,以便更容易处理“仅该选项卡”的代码。有一行像

//-------------------------- begin rules tab methods -------------------

只是不再削减它了。真的,我想我想以某种方式将每个选项卡放在一个单独的文件中。

目前我正在考虑为每个 TTabPage 创建一个 VCL 框架。如果我这样做,我要么需要将所有框架加载到构造函数中的 TPageControl 中,要么在显示选项卡时加载。

这是一个好方法吗?为每个选项卡制作一个完整的 TForm 会更好吗?我应该继续将选项卡包装在 TPageControl 中,还是应该将其更改为 TTabControl(如果内容是动态加载的)?如果这是一个好方法,那么在启动时加载所有选项卡更好,还是每次显示选项卡时加载更好? (如果不完全明显的话,也许优点/缺点在大多数/所有情况下哪个更好)

最佳答案

您可以使用框架或表单。

  • 对于 Frames,您必须添加 TabControl 作为每个 Frame 的父级。
  • 有了表单,您就有 to dock每个表单到 PageControl(表单标题将自动成为 TabControl 标题)。
<小时/>
procedure TMyForm.AddPage( AFormClass : TFormClass );
var
LForm : TForm;
begin
LForm := AFormClass.Create( Self );
LForm.ManualDock( PageControl1, nil, alClient );
LForm.Show;
end;
<小时/>

示例

创建基本设置表单

unit UI_Form_SettingBase;

interface

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

type
TUISettingBase_Form = class( TForm )
private

protected
procedure DoSaveData; virtual;
public
function CanSaveData : Boolean; virtual;
procedure SaveData;
end;

TUISettingBase_FormClass = class of TUISettingBase_Form;

var
UISettingBase_Form : TUISettingBase_Form;

implementation

{$R *.dfm}
{ TUISettingBase_Form }

function TUISettingBase_Form.CanSaveData : Boolean;
begin
Result := True;
end;

procedure TUISettingBase_Form.DoSaveData;
begin

end;

procedure TUISettingBase_Form.SaveData;
begin
if CanSaveData
then
DoSaveData;
end;

end.

从该表单派生所有设置表单并覆盖 DoSaveData 和可选的 CanSaveData 方法

通用设置(带有简单的复选框)

unit UI_Form_SettingCommon;

interface

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

type
TUISettingCommon_Form = class(TUISettingBase_Form)
CheckBox1: TCheckBox;
private

protected
procedure DoSaveData; override;
public

end;

var
UISettingCommon_Form: TUISettingCommon_Form;

implementation

{$R *.dfm}

procedure TUISettingCommon_Form.DoSaveData;
begin
inherited;
// code to save the data
end;

end.

连接设置(使用简单的编辑控件)

unit UI_Form_SettingConnection;

interface

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

type
TUISettingConnection_Form = class( TUISettingBase_Form )
Edit1 : TEdit;
private
protected
procedure DoSaveData; override;
public
end;

var
UISettingConnection_Form : TUISettingConnection_Form;

implementation

{$R *.dfm}
{ TUISettingConnection_Form }

procedure TUISettingConnection_Form.DoSaveData;
begin
inherited;
// code to save the data
end;

end.

将各个部分放在一起:真正的设置表单

主要的设置表单也源自SettingBase

unit UI_Form_Settings;

interface

uses
System.Generics.Collections,
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UI_Form_SettingBase, Vcl.StdCtrls,
Vcl.ExtCtrls, Vcl.ComCtrls;

type
TUISettings_Form = class( TUISettingBase_Form )
PageControl1 : TPageControl;
Panel1 : TPanel;
Save_Button : TButton;
private
FForms : TList<TUISettingBase_Form>;
procedure AddSettingPage( ASettingFormClass : TUISettingBase_FormClass );
protected
procedure DoSaveData; override;
public
function CanSaveData : Boolean; override;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
end;

var
UISettings_Form : TUISettings_Form;

implementation

{$R *.dfm}

uses
UI_Form_SettingCommon, UI_Form_SettingConnection;
{ TUISettings_Form }

procedure TUISettings_Form.AddSettingPage( ASettingFormClass : TUISettingBase_FormClass );
var
LForm : TUISettingBase_Form;
begin
LForm := ASettingFormClass.Create( Self );
try
LForm.ManualDock( PageControl1, nil, alClient );
LForm.Show;
FForms.Add( LForm );
LForm := nil;
finally
LForm.Free;
end;
end;

procedure TUISettings_Form.AfterConstruction;
begin
inherited;
FForms := TList<TUISettingBase_Form>.Create;
// add all the setting forms
AddSettingPage( TUISettingCommon_Form );
AddSettingPage( TUISettingConnection_Form );
end;

procedure TUISettings_Form.BeforeDestruction;
begin
inherited;
FForms.Free;
end;

function TUISettings_Form.CanSaveData : Boolean;
var
LForm : TUISettingBase_Form;
begin
// iterate all setting forms if they can save the data
Result := True;
for LForm in FForms do
Result := Result and LForm.CanSaveData;
end;

procedure TUISettings_Form.DoSaveData;
var
LForm : TUISettingBase_Form;
begin
inherited;
// iterate all setting forms and save the data
for LForm in FForms do
LForm.SaveData;
end;

end.

关于delphi - 如何在 Delphi 中将 TPageControl 选项卡分解为单独的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23944647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com