vcl - How to break up TPageControl tabs into seperate files in Delphi? -


i have vcl form 5 tab tpagecontrol on it. each ttabsheet's content unrelated , has lot of individual controls , processing logic...so want break it's easier deal code "just tab". having line like

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

just isn't cutting anymore. really, think i'd each tab in separate file somehow.

currently i'm contemplating creating vcl frame each ttabpage. if did i'd either need load frames tpagecontrol in constructor, or when tab shown.

is approach? better make whole tform each tab? should continue wrap tabs in tpagecontrol, or should changed ttabcontrol if content loaded dynamically? if approach, better load tabs on startup, or each time tab shown? (perhaps pros/cons if it's not totally obvious better in most/all cases)

you can use either frames or forms.

  • with frames have add tabcontrol parent each frame.
  • with forms have to dock each form pagecontrol (the form caption automatic tabcontrol caption).

procedure tmyform.addpage( aformclass : tformclass ); var   lform : tform; begin   lform := aformclass.create( self );   lform.manualdock( pagecontrol1, nil, alclient );   lform.show; end; 

example

create base settings form

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       dosavedata; end;  end. 

derive settings forms form , override dosavedata , optionally cansavedata methods

common settings (with simple checkbox)

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 save data end;  end. 

connection settings (with simple edit control)

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 save data end;  end. 

putting pieces together: real settings form

the main setting form derived 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;       lform.free;   end; end;  procedure tuisettings_form.afterconstruction; begin   inherited;   fforms := tlist<tuisettingbase_form>.create;   // add 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 setting forms if can save data   result := true;   lform in fforms     result := result , lform.cansavedata; end;  procedure tuisettings_form.dosavedata; var   lform : tuisettingbase_form; begin   inherited;   // iterate setting forms , save data   lform in fforms     lform.savedata; end;  end. 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -