在实际工作中,常遇到对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. 使用VIM + Ctags

    通常在Linux或其他*Nix环境我们都使用VIM作为代码编辑工具,在纯命令终端下,它几乎是无可替代的. 它具有非常强大的扩展机制,在文字编辑方面基本上无所不能. 不过Emacs用户请不要激动,笔者还 ...

  2. jsp中添加弹窗口并且实现向后台双向传递数据

    思路:使用jquery-easyui框架实现弹出div,在jsp初始化时从后台获得数据初始化div中的form表单元素的value属性,从而获得后台数据.在点击确定按钮时使用ajax向后台发送数据. ...

  3. ,gitignore 中规则不起作用的原因

    .gitignore 文件的用途,该文件只能作用于 Untracked Files,也就是那些从来没有被 Git 记录过的文件(自添加以后,从未 add 及 commit 过的文件). 这样则好理解, ...

  4. cocos2d-x box2d使用调试绘图

    cocos2d-x box2d使用调试绘图 复制TestCpp的GLES-Render.h和GLES-Render.cpp过来. 添加一个成员变量: GLESDebugDraw *m_debugDra ...

  5. leetcode算法刷题(三)

    今天在刷了几道简单的动态规划后,又看了看string方面的题 第五题 Longest Palindromic Substring 题目的意思:求一个字符串的最长回文子串 分析:开始,我的想法是,现在字 ...

  6. python作业day4计算器

    思路: 用循环提取最里面的括号,再进行运算 运算时利用正则表达式寻找相应的运算符 先进行乘除,再进行加减 (参考武sir和金角大王的代码) 流程图: 代码: #!/usr/bin/env python ...

  7. Example: Develop Web application on Baidu App Engine using CherryPy

    In the past few months, I have developed two simple applications on Baidu App Engine. Compared to Go ...

  8. java猜数字小游戏

    /* * * 猜数字小游戏 * * 先由系统生成一个2-100之间的随机数字, * * 然后捕获用户从控制台中输入的数字是否与系统生成的随机数字相同, * * 如果相同则统计用户所猜的次数,并给出相应 ...

  9. Qt中添加OpenCV库

    配置在Qt中的OpenCV,看了很多“教程”,最终成功.记一下过程. 本机配置: window7 32位系统: qt-opensource-windows-x86-mingw492-5.5.1: Op ...

  10. 【Itext】7步制作Itext5页眉页脚pdf实现第几页共几页

    itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类 ...