Delphi 实现Ini文件参数与TEdit和TCheckBox绑定(TSimpleParam)

时间:2023-01-09 12:36:42

在实际工作中,常遇到对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 文件操作记要&lpar;1&rpar;&colon; 使用 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. Python爬虫小白入门(二)requests库

    一.前言 为什么要先说Requests库呢,因为这是个功能很强大的网络请求库,可以实现跟浏览器一样发送各种HTTP请求来获取网站的数据.网络上的模块.库.包指的都是同一种东西,所以后文中可能会在不同地 ...

  2. linux之cp&sol;scp命令+scp命令详解&lpar;转&rpar;

    名称:cp 使用权限:所有使用者 使用方式: cp [options] source dest cp [options] source... directory 说明:将一个档案拷贝至另一档案,或将数 ...

  3. 树形DP&plus;二分(Information Disturbing HDU3586)

    题意:给出一颗数,1结点代表司令部,叶子节点代表前线,边全值代表花费,然后需要在某些边放置一些炸弹,炸弹的能量不能小于该边的费用,且炸掉的总费用不能超过m问炸弹能力最小多少, 分析dfs+二分,二分枚 ...

  4. Ubuntu下部署java JDK和eclipse IDE

    安装Java编程开发环境: Ubuntu默认安装openjava,可以通过java -version查看是否安装.但我使用Ubuntu9.10升级到10.04LTS时,openjava没有了.另外,如 ...

  5. USACO3&period;41Closed Fences&lpar;几何)

    这题水的真不易..300多行 累死了 对着数据查错啊 枚举每个边上的点到源点 是否中间隔着别的边  每条边划分500份就够了  注意一下与源点在一条直线上的边不算 几何 啊,,好繁琐 参考各种模版.. ...

  6. 使用 Strace 和 GDB 调试工具的乐趣

    编写 UNIX® 系统程序充满乐趣,并且具有教育意义.使用 UNIX strace 工具和 GDB(GNU 项目调试工具),您可以真正地深入研究系统的功能,并了解组成这些功能的各种各样的程序.同时使用 ...

  7. WPF笔记&lpar;1&period;9 样式和控件模板&rpar;——Hello,WPF!

    原文:WPF笔记(1.9 样式和控件模板)--Hello,WPF! 资源的另一个用途是样式设置: <Window >  <Window.Resources>    <St ...

  8. JAVA 验证码生成(转)

    最近做了一下验证码的功能,网上找了一篇还不错,引用下:http://blog.csdn.net/ruixue0117/article/details/22829557 这篇文章非常好,但是web和js ...

  9. EF Core中DbContext可以被Dispose多次

    我们知道,在EF Core中DbContext用完后要记得调用Dispose方法释放资源.但是其实DbContext可以多次调用Dispose方法,虽然只有第一次Dispose会起作用,但是DbCon ...

  10. vue 深入响应式原理

    vue最显著的特性就是不太引人注意的响应式系统(reactivity system),模型层(model)只是普通的javascript对象,修改它则更新视图view.这会让状态管理变得非常简单且直观 ...