, так я представил решение вопроса. Если что-то не как задумано -- не обессудь. Получилось громоздко, но быстро.
Сделал пустой проект, на нём 2 кнопки. Задал события на нажатие каждой из этих кнопок, на создание формы и на её уничтожение.
При нажатии одной кнопки происходит запись, а на другую повесил считывание пременных из ini.
Code
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses IniFiles;
const
SecName = 'Vars';
var
SL: TStringList;
procedure SetVar(Name: string; Value: string);
begin
SL.Add(Name+'='+Value);
end;
function GetVar(Name: string; Default: string): string;
var
Idx: integer;
begin
Idx := SL.IndexOfName(Name);
if Idx < 0 then begin
Result := Default;
end
else begin
Result := SL.ValueFromIndex[Idx];
end;
end;
procedure LoadVars(FileName: string);
var
IniF: TIniFile;
begin
IniF := TIniFile.Create(FileName);
try
IniF.ReadSectionValues(SecName, SL);
finally
IniF.Free;
end;
end;
procedure SaveVars(FileName: string);
var
IniF: TIniFile;
i: integer;
begin
IniF := TIniFile.Create(FileName);
try
for i := 0 to SL.Count-1 do begin
IniF.WriteString(SecName, SL.Names[i], SL.ValueFromIndex[i]);
end;
finally
IniF.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetVar('G1', '1,2');
SetVar('G2', FloatToStr(33.8));
SetVar('G3', FloatToStr(14/5));
SetVar('F1', '123');
SetVar('F2', IntToStr(456));
SetVar('F3', IntToStr(78 div 9));
SetVar('e1', 'Ïðîâåðêà');
SetVar('e2', '123312312312');
SetVar('e3', 'I am happy!');
SaveVars(ExtractFilePath(ParamStr(0))+'vars.ini');
end;
procedure TForm1.Button2Click(Sender: TObject);
var
G1, G2, G3: real;
F1, F2, F3: integer;
e1, e2, e3: string;
begin
LoadVars(ExtractFilePath(ParamStr(0))+'vars.ini');
G1 := StrToFloat(GetVar('G1', '0'));
G2 := StrToFloat(GetVar('G2', '0'));
G3 := StrToFloat(GetVar('G3', '0'));
F1 := StrToInt(GetVar('F1', '0'));
F2 := StrToInt(GetVar('F2', '-1'));
F3 := StrToInt(GetVar('F3', '123'));
e1 := GetVar('e1', '<empty>');
e2 := GetVar('e2', 'ïóñòî');
e3 := GetVar('e3', 'íåò çíà÷åíèÿ');
ShowMessage(FloatToStr(G1));
ShowMessage(FloatToStr(G2));
ShowMessage(FloatToStr(G3));
ShowMessage(IntToStr(F1));
ShowMessage(IntToStr(F2));
ShowMessage(IntToStr(F3));
ShowMessage(e1);
ShowMessage(e2);
ShowMessage(e3);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SL := TStringList.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
SL.Free;
end;
end.