在实际工作中,常遇到对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. Ubuntu package offline install

    apt-get Use apt-get with the "--print-uris" option to do it. I also add "-qq" so ...

  2. Jquery实现图片切换效果(IE,FF,Goole)都可以正常运行

    这里先对标签的样式进行设置(我这里只用了3张图片,可以根据自己的情况,添加) <style type="text/css"> /*展示图片切换的div样式*/ #Sho ...

  3. [ReactiveCocoa]最简单的RAC入门操作

    当knowckout.js出来的时候,确实被其kobinding惊艳了一下,等到AngularJS出来的时候,把MVVM的模式更是向前推进了一步.所以当ReactiveCocoa出来的时候,也很感兴趣 ...

  4. Android02-Activity01

    1.概念:活动是一种可以包含用户界面的组件, 主要用于和用户进行交互. 2.常见操作:      1.隐藏Activity的标题栏: @Override protected void onCreate ...

  5. 怎查看linux系统的位数

    # uname -a x86_64则说明你是64位内核, 跑的是64位的系统. i386, i686说明你是32位的内核, 跑的是32位的系统

  6. ApiDemos示例学习(2)——App->Activity->Animation

    现在介绍一下com.example.android.app包下的Animation示例. 关键类及函数: ActivityOption overridePendingTransition() make ...

  7. 一步一步学习SignalR进行实时通信_4_Hub

    原文:一步一步学习SignalR进行实时通信_4_Hub 一步一步学习SignalR进行实时通信\_4_Hub SignalR 一步一步学习SignalR进行实时通信_4_Hub 前言 创建Hub 配 ...

  8. MSSQL 日期操作函数 总结

    set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER FUNCTION [dbo].[ufn_getDateOfWeek] (@Date Dateti ...

  9. Javascript 排序数组或对象

    分享一个用于数组或者对象的排序的函数.该函数可以以任意深度的数组或者对象的值作为排序基数对数组或的元素进行排序. 代码如下: /** * 排序数组或者对象 * by Jinko * date 2015 ...

  10. 和Eclipse一起走过的日子

    一见钟情    大二上学期,第一次接触java Web.老师为了帮助我们从底层理解java Web的执行环境,要求我们不能使用不论什么IDE,仅仅能用记事本.    好吧,老师也是为了咱好.简单的一个 ...