内容为:
....
[val]
a1=0.1
a2=0.02
a3=0.05
...
a15=0.03
我要将这些数据读取到form里15个edit里显示出来.我想用循环语句将ini里的值赋值给edit组成的数组
例如 TEdit(Controls[1]).Text:=ini里a1的值;
这语句要怎么写?谢谢!
procedure TForm1.Button1Click(Sender: TObject);
var
A: Array[1..15] of double;
MyIniFile: TIniFile;
I: Integer;
MyIniFile := TIniFile.Create( 'c:\111.ini ');
for I := 0 to Form1.ControlCount -1 do
if Form1.Controls[i].ClassName = TEdit.ClassName then
TEdit(Controls[i]).Text:= MyIniFile.ReadString(' val', floatToStr a(I), ); 这样对吗?
...
另外,读取ini 文件有没有"MyIniFile.double"方式直接读取ini中的小数点值
4 个解决方案
#1
有ReadFloat读取浮点数
在保证TEdit(Controls[i])访问正确的情况下
TEdit(Controls[i]).Text:= MyIniFile.ReadString('val','a'+IntToStr(i),'')
在保证TEdit(Controls[i])访问正确的情况下
TEdit(Controls[i]).Text:= MyIniFile.ReadString('val','a'+IntToStr(i),'')
#2
使用GetPrivateProfileSection
#3
占个位置学习一下
顶顶帖子
学习学习先
顶顶帖子
学习学习先
#4
五个Edit,Edit1-Edit5,ini文件有五个值a1-a5,
config.ini
config.ini
[val]
a1=0.1
a2=0.2
a3=0.3
a4=0.4
a5=0.5
unit unitMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IniFiles, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetEditValue(idx: Byte): String;
const
K_IniName = 'config.ini';
K_KeyName = 'val';
K_Prefix = 'a';
begin
with TIniFile.Create(ExtractFilePath(Application.ExeName)+K_IniName) do
try
Result := ReadString(K_KeyName,K_Prefix+IntToStr(idx),'0');
finally
free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
aControl: TControl;
aEdit: TEdit;
i,k: Integer;
begin
for i := 0 to Self.ControlCount - 1 do
if Self.Controls[i] is TEdit then
begin
aEdit := TEdit(Controls[i]);
k := StrToInt(Copy(aEdit.Name,Length(aEdit.Name),1));
aEdit.Text := GetEditValue(k);
end;
end;
end.
#1
有ReadFloat读取浮点数
在保证TEdit(Controls[i])访问正确的情况下
TEdit(Controls[i]).Text:= MyIniFile.ReadString('val','a'+IntToStr(i),'')
在保证TEdit(Controls[i])访问正确的情况下
TEdit(Controls[i]).Text:= MyIniFile.ReadString('val','a'+IntToStr(i),'')
#2
使用GetPrivateProfileSection
#3
占个位置学习一下
顶顶帖子
学习学习先
顶顶帖子
学习学习先
#4
五个Edit,Edit1-Edit5,ini文件有五个值a1-a5,
config.ini
config.ini
[val]
a1=0.1
a2=0.2
a3=0.3
a4=0.4
a5=0.5
unit unitMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IniFiles, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetEditValue(idx: Byte): String;
const
K_IniName = 'config.ini';
K_KeyName = 'val';
K_Prefix = 'a';
begin
with TIniFile.Create(ExtractFilePath(Application.ExeName)+K_IniName) do
try
Result := ReadString(K_KeyName,K_Prefix+IntToStr(idx),'0');
finally
free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
aControl: TControl;
aEdit: TEdit;
i,k: Integer;
begin
for i := 0 to Self.ControlCount - 1 do
if Self.Controls[i] is TEdit then
begin
aEdit := TEdit(Controls[i]);
k := StrToInt(Copy(aEdit.Name,Length(aEdit.Name),1));
aEdit.Text := GetEditValue(k);
end;
end;
end.