Delphi implementation INI file parameters with Tedit and Tcheckbox bindings (Tsimpleparam)

Source: Internet
Author: User

This routine runs under the Delphi XE8 version

Delphi Group: 59129236

Delphi as the work of auxiliary skills, towards a happy life!

Xiao qq:26562729


Simpleparamdemo Features:

The parameters of the INI file are bound to the controls for ease of use.

This routine has a total of 2 units

usimpleparam->tsimpleparam;//this feature

usimplelist->tsimplelist; implementation of Tlist with generics, a bit more practical

SOURCE Download: Http://files.cnblogs.com/files/lackey/SimpleParamDemo.zip

Usage:

Const

Cseditsimpleparam = ' Editsimpleparam ';
Cscheckboxparam = ' Checkboxparam ';


Finifile: = Tinifile.create ('. \simpleparams.ini ');
Fsimpleparam: = tsimpelparam.create;

Fsimpleparam.inifile: = Finifile; Specify IniFile

Bind the control and set default parameters
Fsimpleparam.binding (Edtsimpleparam, Cseditsimpleparam). Setinteger (101);
Fsimpleparam.binding (Chksimpleparam, Cscheckboxparam). SetBoolean (TRUE);

Load parameters
Fsimpleparam.loadparams;

Fsimpleparam.saveparams; Save parameters

Get parameters
Nsimpleparam: = Fsimpleparam[cseditsimpleparam]. Asinteger;
Bsimleparam: = Fsimpleparam[cscheckboxparam]. Asboolean;

Setting parameters
Fsimpleparam[cseditsimpleparam]. Setinteger (105);
Fsimpleparam[cscheckboxparam]. SetBoolean (FALSE);

Unit usimpleparam;interfaceuses usimplelist, Generics.collections, Vcl.stdctrls, Vcl.controls, IniFiles;type  Tdefaulttype = (Dtinteger, dtstring, Dtboolean);    Tbaseparam<t:twincontrol> = class private fctrl:t;    findent:string;    Fdefaulttype:tdefaulttype;    Fdefaultinteger:integer;    fdefaultstring:string;  Fdefaultboolean:boolean; Public function Asinteger:integer;    Virtual function asstring:string;    Virtual function Asboolean:boolean;    Virtual Procedure Setinteger (Val:integer);    Virtual Procedure SetString (val:string);    Virtual Procedure SetBoolean (Val:boolean);    Virtual    Procedure Setdefaultinteger (Val:integer);    Procedure setdefaultstring (val:string);    Procedure Setdefaultboolean (Val:boolean); Procedure Binding (actrl:t; aindent:string);  Virtual  End Teditparam = Class (tbaseparam<tedit>) public function Asinteger:integer;    Override function asstring:string;    Override Procedure Setinteger (Val:integER);    Override Procedure SetString (val:string);    Override Procedure Binding (Actrl:tedit; aindent:string);  Override  End Tcheckboxparam = Class (tbaseparam<tcheckbox>) public function Asboolean:boolean;    Override Procedure SetBoolean (Val:boolean);    Override Procedure Binding (Actrl:tcheckbox; aindent:string);  Override  End Tbaseparamlist = Class (Tclasssimplelist < Tbaseparam < Twincontrol >>) public function Binding (ACTRL:TWI Ncontrol;  aindent:string): tbaseparam<twincontrol>;  End  Tsimpelparam = Class Fparamlist:tbaseparamlist;    Private Finifile:tinifile;    fsectionindent:string;    Procedure Setinifile (Val:tinifile);    Procedure Setsectionindent (val:string);  function Get (aindent:string): tbaseparam<twincontrol>;    Public constructor Create; destructor Destroy;    Override    Property Inifile:tinifile read Finifile write setinifile; Property sectionindent:string Read Fsectionindent write SetsectioNindent; Property items[aindent:string]: tbaseparam<twincontrol> Read Get;    Default function Binding (Actrl:twincontrol;    aindent:string): tbaseparam<twincontrol>;    Procedure Loadparams;  Procedure Saveparams; End;implementationuses sysutils;function teditparam.asinteger:integer;begin Result: = Strtointdef (FCtrl.Text, 0); end function Teditparam.asstring:string;begin Result: = Fctrl.text;end;procedure teditparam.binding (actrl:tedit;  aindent:string); Begin Inherited;end;procedure Teditparam.setinteger (Val:integer); Begin inherited;  If Assigned (Fctrl) then fctrl.text: = IntToStr (val); End;procedure teditparam.setstring (val:string); Begin inherited; If Assigned (Fctrl) then fctrl.text: = Val;end;function tcheckboxparam.asboolean:boolean;begin Result: = Fctrl.checke D;end;procedure tcheckboxparam.binding (Actrl:tcheckbox;  aindent:string); Begin Inherited;end;procedure Tcheckboxparam.setboolean (Val:boolean); Begin inherited; If Assigned (Fctrl) Then   fctrl.checked: = Val;end; {tbaseparam<t>}function tbaseparam<t>. Asboolean:boolean;beginend;function Tbaseparam<t>. Asinteger:integer;beginend;function Tbaseparam<t>. Asstring:string;beginend;procedure Tbaseparam<t>. Binding (actrl:t;  aindent:string); begin Fctrl: = Actrl; Findent: = Aindent;end;function tbaseparamlist.binding (actrl:twincontrol;  aindent:string): Tbaseparam<twincontrol>;begin Result: = nil; If Actrl is Tedit then Result: = tbaseparam<twincontrol> (teditparam.create) Else if Actrl are tcheckbox then R  Esult: = tbaseparam<twincontrol> (tcheckboxparam.create);    If Assigned (Result) THEN begin Result.binding (Actrl, aindent); ADD (Result) end;end;procedure tbaseparam<t>. SetBoolean (Val:boolean); Beginend;procedure tbaseparam<t>.  Setdefaultboolean (Val:boolean); begin Fdefaultboolean: = val; Fdefaulttype: = Dtboolean;end;procedure tbaseparam<t>. Setdefaultinteger (Val:integer); Begin FdefaultintEger: = val; Fdefaulttype: = Dtinteger;end;procedure tbaseparam<t>.  Setdefaultstring (val:string); begin fdefaultstring: = val; Fdefaulttype: = Dtstring;end;procedure tbaseparam<t>. Setinteger (Val:integer); Beginend;procedure tbaseparam<t>. SetString (val:string); beginend; {Tsimpelparam}function tsimpelparam.binding (Actrl:twincontrol; aindent:string): Tbaseparam<twincontrol>;begin Result: = fparamlist.binding (Actrl, aindent); end;constructor  Tsimpelparam.create;begin inherited;  Fparamlist: = tbaseparamlist.create;  Fsectionindent: = ' Main '; end;destructor tsimpelparam.destroy;begin fparamlist.free; Inherited;end;function Tsimpelparam.get (aindent:string): Tbaseparam<twincontrol>;var i:integer;begin Result:  = nil; For I: = 0 to Fparamlist.count-1 do BEGIN if Sametext (Aindent, Fparamlist[i].      findent) THEN begin Result: = Fparamlist[i];    Exit  End End;end;procedure Tsimpelparam.loadparams;var B:tbaseparam<twincontrol>;beGin if Assigned (finifile) THEN begin for B in Fparamlist do begin if B.classname = ' Teditparam ' then beg In If B.fdefaulttype = Dtstring then BEGIN Tedit (B.fctrl).        Text: = inifile.readstring (Fsectionindent, b.findent, b.fdefaultstring); End else If B.fdefaulttype = Dtinteger then BEGIN Tedit (B.fctrl).        Text: = IntToStr (Inifile.readinteger (fsectionindent, B.findent, B.fdefaultinteger));      End End else If b.classname = ' Tcheckboxparam ' THEN begin Tcheckbox (B.fctrl).      Checked: = Inifile.readbool (Fsectionindent, b.findent, B.fdefaultboolean);    End  End    End;end;procedure Tsimpelparam.saveparams;var B:tbaseparam<twincontrol>;begin if Assigned (FIniFile) THEN BEGIN For B in fparamlist does begin if B.classname = ' Teditparam ' THEN begin Finifile.writestring (fsection Indent, B.findent, Tedit (B.fctrl).      Text); End else If b.classname = ' TCHeckboxparam ' THEN begin Finifile.writebool (Fsectionindent, B.findent, Tcheckbox (B.fctrl).      Checked);    End  End End;end;procedure Tsimpelparam.setinifile (val:tinifile); begin Finifile: = Val;end;procedure Tsimpelparam.setsectionindent (val:string); begin fsectionindent: = Val;end;end.

Unit usimplelist;interfaceuses Generics.collections;type tsimplelist<t> = Class (tlist<t>) Private FCurIn    Dexpos:integer;    function Dopopbyindex (Index:integer): T;    Procedure Freeallitems;  Procedure Setcurindexpos (const value:integer);    protected Fneedfreeitem:boolean; Procedure Freeitem (item:t);  Virtual    Public constructor Create; destructor Destroy;    Override    Procedure Lock;    Procedure Unlock;    function Popfirst:t;    function Poplast:t;    function Popbyindex (Index:integer): T;    Procedure Clearandfreeallitems;  Property Curindexpos:integer read Fcurindexpos write Setcurindexpos;  End Tclasssimplelist<t:class, constructor> = Class (tsimplelist<t>) protected procedure Freeitem (item:t);    Override  function Addnewone:t; End;implementationprocedure Tsimplelist<t>.  Clearandfreeallitems;begin Freeallitems; Clear;end;constructor Tsimplelist<t>.  Create;begin inherited;  Fneedfreeitem: = true; FcurindExpos: = -1;end;destructor tsimplelist<t>.  Destroy;begin Freeallitems; Inherited;end;function Tsimplelist<t>. Dopopbyindex (Index:integer): T;begin if (index >= 0) and (index <= count-1) THEN BEGIN result: = Items[index    ];    Delete (index);  Exit;  End Result: = T (nil); End;procedure Tsimplelist<t>.    Freeallitems;var Item:t;begin If Fneedfreeitem then begin fcurindexpos: =-1;  For item in the Self do Freeitem (Item); End;end;procedure Tsimplelist<t>.  Freeitem (item:t); Begin//Assuming T is Pmyrec =^tmyrec Tmyrec=record;  Is this the right wording?  If Gettypekind (T) = Tkpointer then/begin//Dispose (Pointer (Pointer (@Item) ^)); End;end;procedure Tsimplelist<t>. Lock;begin System. Tmonitor.enter (self); end;procedure tsimplelist<t>. Unlock;begin System. Tmonitor.exit (self); end;function tsimplelist<t>. Popbyindex (Index:integer): t;begin Result: = Dopopbyindex (Index); end;function tsimplelist<t>. Popfirst:t;begin Result: = dopopbyiNdex (0); end;function tsimplelist<t>. Poplast:t;begin Result: = Dopopbyindex (count-1); End;procedure tsimplelist<t>. Setcurindexpos (const value:integer); begin fcurindexpos: = Value;end; {tthreadclasslist<t>}function tclasssimplelist<t>.  Addnewone:t;begin Result: = T.create (); ADD (result); End;procedure tclasssimplelist<t>. Freeitem (item:t); Begin Item.free;end;end.

  

Delphi implementation INI file parameters with Tedit and Tcheckbox bindings (Tsimpleparam)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.