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

前端之家收集整理的这篇文章主要介绍了如何将TPageControl选项卡分解为Delphi中的单独文件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个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方法

常用设置(使用简单的CheckBox)

unit UI_Form_SettingCommon;

interface

uses
  Winapi.Windows,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,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,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,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.
原文链接:https://www.f2er.com/delphi/102066.html

猜你在找的Delphi相关文章