Title: Simple automated saving to ini
I have automated a system of saving to ini
files which allows you to save to an ini
file merely by adding an edit or other control
to a given panel or group box.
Add the unit below to your project, and follow
the instructions.
unit MyIniU;
// Author: Russell Crosser
// rcrosser@netlink.com.au
interface
uses ExtCtrls, Controls, Classes;
procedure LoadFromIni(fn: string; var CC: TCustomControl);
procedure SaveToIni(fn: string; var CC: TCustomControl);
(*** Calling SaveToIni and LoadFromIni
1. Place a Panel or GroupBox onto the form where convenient
2. Name the control xxxxIni_ssssssss
where xxxx is anything (except 'Ini_') - I use Panel or GroupBox
and ssssssss is anything unique to this control - but this is used
as the name of the section in the ini file
e.g. PanelIni_DefaultNames --- [Default Names] as the section
GroupBoxIni_StartupValues - [Startup Values] as the section
3. Onto the Panel or GroupBox place any number of controls e.g Edit,
CheckBox etc (any that are named below).
4. Each control to be named yyyy_kkkkkkkk
where ssss is anything (not including '_') I use the control type
and kkkkkkkk is the name of the key to be written.
e.g. Edit_BackupDir ---- BackupDir=Value where Value is the value
of the Text property of the Edit control
Panel_BackgroundColour ---- BackgroundColour=Value where Value
is the value of the color property of the panel.
5. Call the procedures as follows:
uses: MyIniU;
LoadFromIni(s,TCustomControl(PanelIni_DefaultNames));
SaveToIni(s,TCustomControl(PanelIni_DefaultNames));
6. Use on the main form, or have an options form, with the two calls above
in the OnCreate and OnClose methods of the form, respectively.
Note that I have chosen certain properties of each control type to save. This
could easily be modified by recoding parts of the unit. Further control
types may be added to this implementation.
If you wish to add a value or two to the Ini file, just add appropriately
named controls to the panel.
To save to a different ini file, add a 2nd or 3rd panel or group box. Each
must be saved and loaded separately, with a call to SaveToIni or
LoadFromIni. Two panels may operate with the same ini file, and may
use the same or separate sections and keys. If they use the same sections
and keys, then they will overwrite the previously saved values.
***)
implementation
uses SysUtils, StdCtrls, ComCtrls, Grids, IniFiles;
function BoolToChr(b: boolean): char;
begin
if b then Result := 'Y' else Result := 'N';
end;
function ChrToBool(ch: char): boolean;
begin
Result := ch in ['Y','y','T','t'];
end;
function GetString(Name, s: string): string;
var
i: integer;
begin
i := pos(s,Name);
if i 0 then begin
Result := copy(Name,i + Length(s), length(Name));
for i := length(Result) downto 2 do begin
if Result LowerCase(Result) then insert(' ',Result,i);
end;
end else Result := '';
end;
procedure LoadFromIni(fn: string; var CC: TCustomControl);
var
StoreIni: TIniFile;
v: Longint;
code,
i, j, Cont: integer;
Trol: TControl;
s, t, IniSection: string;
begin
StoreIni := TIniFile.Create(fn);
IniSection := GetString(CC.Name, 'Ini_');
for Cont := 0 to CC.ControlCount - 1 do begin
Trol := CC.Controls[Cont];
if Pos('_',Trol.Name) 0 then begin
////// TEdit //////
if (Trol is TEdit)
then TEdit(Trol).Text :=
StoreIni.ReadString(IniSection,
GetString(Trol.Name, '_'),'')
////// TLabel //////
else if (Trol is TLabel)
then TLabel(Trol).Caption :=
StoreIni.ReadString(IniSection,
GetString(Trol.Name, '_'),'')
////// TDateTimePicker //////
else if (Trol is TDateTimePicker)
then begin
s := trim(StoreIni.ReadString(IniSection,
GetString(Trol.Name, '_'),''));
if s '' then TDateTimePicker(Trol).Date := StrToDate(s);
////// TPanel //////
end else if (Trol is TPanel)
then begin
t := GetString(Trol.Name, '_');
s := StoreIni.ReadString(IniSection,
t,'$ffffffff');
val(s,v,code);
if code 0 then v := 1000;
TPanel(Trol).Color := v;
end
////// TUpDown //////
else if (Trol is TUpDown)
then begin
s := StoreIni.ReadString(IniSection,
GetString(Trol.Name, '_'),'4');
val(s,v,code);
if code 0 then v := 4;
TUpDown(Trol).Position := v;
end
////// TComboBox //////
else if (Trol is TComboBox)
then begin
s := StoreIni.ReadString(IniSection,
GetString(Trol.Name, '_'),'0');
val(s,v,code);
if code 0 then v := 0;
TComboBox(Trol).ItemIndex := v;
end
////// TMemo //////
else if (Trol is TMemo)
then begin
TMemo(Trol).Clear;
s := StoreIni.ReadString(IniSection, GetString(Trol.Name, '_'), '');
s := copy(s,2,length(s)-2);
TMemo(Trol).Lines.CommaText := s;
end
////// TListBox //////
else if (Trol is TListBox)
then begin
TListBox(Trol).Clear;
s := StoreIni.ReadString(IniSection, GetString(Trol.Name, '_'), '');
s := copy(s,2,length(s)-2);
TListBox(Trol).Items.CommaText := s;
end
////// TStringGrid //////
else if (Trol is TStringGrid)
then for i := 0 to TStringGrid(Trol).ColCount - 1 do
begin
s := StoreIni.ReadString(IniSection,
GetString(Trol.Name, '_') + IntToStr(i+1), '');
s := copy(s,2,length(s)-2);
TStringGrid(Trol).Cols.CommaText := s;
for j := 0 to TStringGrid(Trol).ColCount - 1 do
TStringGrid(Trol).Cells[j,i] :=
trim(TStringGrid(Trol).Cells[j,i]);
end
////// TCheckBox //////
else if (Trol is TCheckBox)
then begin
s := StoreIni.ReadString(IniSection,
GetString(Trol.Name, '_'), 'N') + 'N';
TCheckBox(Trol).Checked := ChrToBool(s[1]);
TCheckBox(Trol).AllowGrayed := false;
end;
end;
end;
StoreIni.Free;
end;
procedure SaveToIni(fn: string; var CC: TCustomControl);
var
StoreIni: TIniFile;
i, {j, }Comp: integer;
Trol: TControl;
IniSection: string;
begin
StoreIni := TIniFile.Create(fn);
IniSection := GetString(CC.Name, 'Ini_');
for Comp := 0 to CC.ControlCount - 1 do begin
Trol := CC.Controls[Comp];
if Pos('_',Trol.Name) 0 then begin
////// TEdit //////
if (Trol is TEdit)
then StoreIni.WriteString(IniSection,
GetString(Trol.Name, '_'),TEdit(Trol).Text)
////// TLabel //////
else if (Trol is TLabel)
then StoreIni.WriteString(IniSection,
GetString(Trol.Name, '_'),TLabel(Trol).Caption)
////// TDateTimePicker //////
else if (Trol is TDateTimePicker)
then StoreIni.WriteString(IniSection,
GetString(Trol.Name, '_'),
FormatDateTime(ShortDateFormat,TDateTimePicker(Trol).Date))
////// TPanel //////
else if (Trol is TPanel)
then StoreIni.WriteString(IniSection,
GetString(Trol.Name, '_'),'$' + inttohex(TPanel(Trol).Color,6))
////// TUpDown //////
else if (Trol is TUpDown)
then StoreIni.WriteString(IniSection,
GetString(Trol.Name, '_'),inttostr(TUpDown(Trol).Position))
////// TComboBox //////
else if (Trol is TComboBox)
then StoreIni.WriteString(IniSection,
GetString(Trol.Name, '_'),inttostr(TComboBox(Trol).ItemIndex))
////// TMemo //////
else if (Trol is TMemo)
then StoreIni.WriteString(IniSection,
GetString(Trol.Name, '_'),
'[' + TMemo(Trol).Lines.CommaText + ']')
////// TListBox //////
else if (Trol is TListBox)
then begin
StoreIni.WriteString(IniSection,
GetString(Trol.Name, '_'),
'[' + TListBox(Trol).Items.CommaText + ']');
end
////// TStringGrid //////
else if (Trol is TStringGrid)
then for i := 0 to TStringGrid(Trol).ColCount - 1 do begin
StoreIni.WriteString(IniSection,
GetString(Trol.Name, '_') + IntToStr(i+1),
'[' + TStringGrid(Trol).Cols.CommaText + ']');
end
////// TCheckBox //////
else if (Trol is TCheckBox) then begin
if TCheckBox(Trol).State cbGrayed
then StoreIni.WriteString(IniSection,
GetString(Trol.Name, '_'),
BoolToChr(TCheckBox(Trol).Checked))
end;
end;
end;
StoreIni.Free;
end;
end.