在实际工作中,常遇到对Ini参数的修改,显示与保存问题。

如果手工写代码,会有很大的工作量。

本例把ini文件的参数与控件绑定起来,以达到方便使用的目的。

本例程共有2个单元

uSimpleParam->TSimpleParam;//本功能

uSimpleList->TSimpleList;用泛型实现的TList,更实用一点

源码下载

 //用法:

 const

   csEditSimpleParam = 'EditSimpleParam';
csCheckBoxParam = 'CheckBoxParam'; FIniFile := TIniFile.Create('.\SimpleParams.ini');
FSimpleParam := TSimpelParam.Create; FSimpleParam.IniFile := FIniFile; // 指定 IniFile // 绑定控件,并设置默认参数
FSimpleParam.Binding(edtSimpleParam, csEditSimpleParam).SetInteger();
FSimpleParam.Binding(chkSimpleParam, csCheckBoxParam).SetBoolean(true); // 加载参数
FSimpleParam.LoadParams; FSimpleParam.SaveParams; // 保存参数 //获取参数
nSimpleParam := FSimpleParam[csEditSimpleParam].AsInteger;
bSimleParam := FSimpleParam[csCheckBoxParam].AsBoolean; //设置参数
FSimpleParam[csEditSimpleParam].SetInteger();
FSimpleParam[csCheckBoxParam].SetBoolean(false);
 unit uSimpleParam;

 interface

 uses
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: TWinControl; 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; implementation uses
SysUtils; function TEditParam.AsInteger: integer;
begin
Result := StrToIntDef(FCtrl.Text, );
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.Checked;
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;
begin end; function TBaseParam<T>.AsInteger: integer;
begin end; function TBaseParam<T>.AsString: string;
begin end; 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 is TCheckBox then
Result := TBaseParam<TWinControl>(TCheckBoxParam.Create); if Assigned(Result) then
begin
Result.Binding(ACtrl, AIndent);
Add(Result)
end; end; procedure TBaseParam<T>.SetBoolean(val: boolean);
begin end; 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);
begin
end; procedure TBaseParam<T>.SetString(val: string);
begin end; { 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 := to FParamList.Count - 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
begin 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 do
begin
if B.ClassName = 'TEditParam' then
begin
FIniFile.WriteString(FSectionIndent, 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.

uSimpleParam.pas

代码略长,请耐心看。可以当成面向对象的基础用法来学习。

附:delphi 进阶基础技能说明

Delphi 实现Ini文件参数与TEdit和TCheckBox绑定(TSimpleParam)的更多相关文章

  1. Delphi对ini文件的操作

    一.INI文件的结构:; 注释[小节名]关键字=值 INI文件允许有多个小节,每个小节又允许有多个关键字, “=”后面是该关键字的值. 值的类型有三种:字符串.整型数值和布尔值.其中字符串存贮在INI ...

  2. delphi读取ini文件

    ini文件在系统配置及应用程序参数保存与设置方面,具有很重要的作用,所以可视化的编程一族,如vb.vc.vfp.delphi等都提供了读写ini文件的方法,其中delphi中操作ini文件,最为简洁, ...

  3. delphi对ini文件的操作(转载 万一)

    ini 文件操作记要(1): 使用 TIniFileunit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Gr ...

  4. Delphi 对ini文件的操作

    界面如图: 代码如下: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Vari ...

  5. Delphi操作Ini文件

    Delphi提供了一个TInifile类,使我们可以非常灵活的处理INI文件 一.INI文件的结构[小节名]ini文件       关键字1=值1       关键子2=值2INI文件允许有多个小节, ...

  6. delphi中ini 文件操作记要(1): 使用 TIniFile

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  7. delphi xe4 ini文件不能读取的解决方法

    今天发现用inifiles下 tinifile.readstring方法突然不能读数据了,结果把ini文件格式由utf-8改成unicode后就能正常读取了.

  8. 封装 INI 文件读写函数

    delphi读写ini文件实例 //--两个过程,主要实现:窗体关闭的时候,文件保存界面信息:窗体创建的时候,程序读取文件文件保存的信息. //--首先要uses IniFiles(单元) //--窗 ...

  9. delphi INI文件

    INI 文件读写 filecreate('路径加文件名')://创建一个文件. (1) INI文件的结构: ;这是关于INI文件的注释部分 [节点] 关键字=值 ... INI文件允许有多个节点,每个 ...

随机推荐

  1. MySql的like语句中的通配符:百分号、下划线和escape 的使用

    MySql的like语句中的通配符:百分号.下划线和escape %代表任意多个字符 select * from user where username like '%huxiao'; select ...

  2. 获取ajax对象

    function ajaxFunction(){ var xmlHttp; try{ // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest ...

  3. vs2012 发布网站丢失文件

    问题描述 在发布网站时,发现上线的网站总是功能缺失,而本地代码确实没问题. 到发布网站的磁盘去查看,发现丢失了很多静态页面文件. 这是一个很奇怪的问题:mvc的网站,丢失了很多View(大部分的csh ...

  4. MySQL----基本数据类型

    1.数值型: tinyint:1字节 smallint:2字节 mediumint:3字节 int:4字节 bigint:8字节 float:4字节 double:8字节 decimal(m,c):m ...

  5. 算法分析-动态规划(cut_rod)

    什么是动态规划,我们要如何描述它? 动态规划算法通常基于一个递推公式及一个或多个初始状态. 当前子问题的解将由上一次子问题的解推出.使用动态规划来解题只需要多项式时间复杂度, 因此它比回溯法.暴力法等 ...

  6. 博士论文》》》 Journal,magazine,transaction,proceeding

    Journal期刊:刊登关于某特殊主题的文章的期刊 magazine杂志:综合性内容的期刊 transactions(学会等的)议事录,会报,会刊 proceedings记录, 会议录; 年[学]报; ...

  7. 手工制作OTG连接线 让小白实现OTG功能

    说到OTG功能,很重要的一点是,现在不少网上的720P高清视频体积已经超过4GB,我的小白也支持exFat及NTFS磁盘格式,可存储播放大于4GB的高清影音文件,也能通过OTG读取播放NTFS格式U盘 ...

  8. 2014上半年acm总结(1)(入门+校赛)

    大一下学期才开始了acm,不得不说有一点迟,但是acm确实使我的生活充实了很多,,不至于像以前一样经常没事干=  = 上学期的颓废使我的c语言学的渣的一笔..靠考前突击才基本掌握了语法 寒假突然醒悟, ...

  9. xshell + xmanger连接centos gnome+ kde桌面 for需要X window的App

  10. Linux是什么

    计算机主要以二进制为单位,而目前常用的磁盘容量单位为B,其单位换算为1B=8bit,其他的以1024为其倍数,如1GB=1024MB等. 操作系统(Operation System)主要用于管理与驱动 ...