在实际工作中,常遇到对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. html 中head显示 在标题栏里面的图片

    在<head>标签里加<link rel="Shortcut Icon" href="你的ico图片地址" /> 一般标题栏里的图片是1 ...

  2. 35个jQuery小技巧!

    1. 禁止右键点击$(document).ready(function(){    $(document).bind("contextmenu",function(e){     ...

  3. 第二章——Parcelable接口的使用(跨进程,Intent传输)

    一.Parcelable类(Android独有的) 简介:Parcelable是一个接口. 作用:是Android提供的序列化接口,实现序列化和反序列化的操作. 二.跨进程使用 步骤一:创建Book类 ...

  4. sshd被攻击的自动防御方法v2

      1.增加了“频繁攻击的封锁时间”,即设置为上次攻击时间的2倍 2.加入了数据库支持,将攻击者相关信息记录入库,如攻击者ip.攻击次数.封锁时间 3.简化了代码   具体实现步骤如下:   1.创建 ...

  5. 大型网站性能优化(页面(HTML)优化的方法)

    页面(HTML)优化的方法 除了语言层面上进行优化外,对Web开发,HTML的优化将很大程度上减轻服务器的负载,提高网站的性能 1). 减少HTTP请求数.打开网页,浏览器会发出很多请求,图片,脚本, ...

  6. TPen的7种Style和16种Mode

    //TPen 的主要属性有四: Color.Width.Style.Mode {Color: 颜色} {Width: 宽度; 默认是 1; 如果赋予 <= 0 的值, 会使用默认值} {Styl ...

  7. javascript函数值的重写

    原文:javascript函数值的重写 javascript函数值的重写 定义了一个函数,需要重写这个函数并使用原先的函数值.做法是: 1.定义一个变量让原先函数的值指向它,把原先函数的指向一个新的函 ...

  8. 快学Scala习题解答—第一章 基础

    1 简介 近期对Scala比较感兴趣,买了本<快学Scala>,感觉不错.比<Programming Scala:Tackle Multi-Core Complexity on th ...

  9. Linux下查找最大文件

    当我们应用一段时间以后,Linux可能会变得臃肿了,那么,怎么找出一个“path”下的最大文件呢? 可以使用du命令,如: du -sh [dirname|filename] 如:当前目录的大小: d ...

  10. Expect:100-Continue & HTTP 417 Expectation[转]

    Expect:100-Continue & HTTP 417 Expectation 背景:今天调试火车票查询的代码,发现一个奇怪的事情,如果使用公司本地的代理,那么一切正常,如果使用的是公司 ...