用一个Edit显示该变量内容?我不明白不是有好多变量吗?以下方法是将变量内容和变量一起显示出来:var a:TIniFile; i,j:Integer; st,value:TStringList; tmpNode,child:TTreeNode; tmpstr:String; begin a:=TIniFile.Create(文件名称); st:=TStringList.Create; value:=TStringList.Create; a.ReadSections(st); for i:=0 to st.Count-1 do begin tmpNode:=TreeView1.Items.Add(nil,st.Strings[i]); a.ReadSection(st.Strings[i],value); for j:=0 to value.Count-1 do begin tmpStr:=a.ReadString(st.Strings[i],value.Strings[j],''); Child:=TreeView1.Items.AddChild(tmpNode,value.Strings[j]+'='+tmpstr); end; end; value.Free; st.Free; a.Free; end;
haoco(程序员) 写的代码不错!
窗体上添加一个Button1,一个Edit1,一个TreeView1; 程序uses IniFilesprocedure TForm1.Button1Click(Sender: TObject); var MyIniFile:TInifile; StrSections,StrSection:TStringList; i_Loop,j_Loop:Integer; ParentNode,ChildNode:TTreeNode; ParentCaption,ChildCaption:String; ParentVal,ChildVal:^String; Flag:integer; begin TreeView1.Items.Clear; Flag:=0; Try MyIniFile:=TIniFile.Create('d:\test.ini'); StrSections:=TStringList.Create; MyIniFile.ReadSections(StrSections); for i_Loop := 0 to StrSections.Count - 1 do begin Flag:=1; ParentCaption:=StrSections.Strings[i_Loop]; Try StrSection:=TStringList.Create; MyIniFile.ReadSection(ParentCaption,StrSection); New(ParentVal); ParentVal^:='这是段,有' + IntToStr(StrSection.Count) + '个变量'; ParentNode:=TreeView1.Items.Add(nil,'ParentCaption'); ParentNode.Text:=ParentCaption; ParentNode.Data:=ParentVal; for j_Loop := 0 to StrSection.Count - 1 do begin Flag:=2; ChildCaption:=StrSection.Strings[j_Loop]; New(ChildVal); ChildVal^:=MyIniFile.ReadString(StrSections[i_Loop],StrSection[j_Loop],''); ChildNode:=TreeView1.Items.AddChild(ParentNode,ChildCaption); ChildNode.Data:=ChildVal; end; Finally StrSection.Free; end; end; Finally if Flag > 1 then begin Dispose(ParentVal); Dispose(ChildVal); end else if Flag>0 then Dispose(ParentVal); StrSections.Free; MyIniFile.Free; end; end;procedure TForm1.TreeView1Click(Sender: TObject); var MyTreeNode:TTreeNode; sVal:^String; begin if (TreeView1.Items.Count<=0) or (TreeView1.Selected=nil) then Exit; MyTreeNode:=TreeView1.Selected; sVal:=MyTreeNode.Data; Edit1.Text:=sVal^; end;
引用lincanwen(密码错误): 窗体上添加一个Button1,一个Edit1,一个TreeView1; 程序uses IniFilesprocedure TForm1.Button1Click(Sender: TObject); var MyIniFile:TInifile; StrSections,StrSection:TStringList; i_Loop,j_Loop:Integer; ParentNode,ChildNode:TTreeNode; ParentCaption,ChildCaption:String; ParentVal,ChildVal:^String; Flag:integer; begin TreeView1.Items.Clear; Flag:=0; Try MyIniFile:=TIniFile.Create('d:\test.ini'); StrSections:=TStringList.Create; MyIniFile.ReadSections(StrSections); for i_Loop := 0 to StrSections.Count - 1 do begin Flag:=1; ParentCaption:=StrSections.Strings[i_Loop]; Try StrSection:=TStringList.Create; MyIniFile.ReadSection(ParentCaption,StrSection); New(ParentVal); ParentVal^:='这是段,有' + IntToStr(StrSection.Count) + '个变量'; ParentNode:=TreeView1.Items.Add(nil,'ParentCaption'); ParentNode.Text:=ParentCaption; ParentNode.Data:=ParentVal; for j_Loop := 0 to StrSection.Count - 1 do begin Flag:=2; ChildCaption:=StrSection.Strings[j_Loop]; New(ChildVal); ChildVal^:=MyIniFile.ReadString(StrSections[i_Loop],StrSection[j_Loop],''); ChildNode:=TreeView1.Items.AddChild(ParentNode,ChildCaption); ChildNode.Data:=ChildVal; end; Finally StrSection.Free; end; end; Finally if Flag > 1 then begin Dispose(ParentVal); Dispose(ChildVal); end else if Flag>0 then Dispose(ParentVal); StrSections.Free; MyIniFile.Free; end; end;procedure TForm1.TreeView1Click(Sender: TObject); var MyTreeNode:TTreeNode; sVal:^String; begin if (TreeView1.Items.Count<=0) or (TreeView1.Selected=nil) then Exit; MyTreeNode:=TreeView1.Selected; sVal:=MyTreeNode.Data; Edit1.Text:=sVal^; end;
a:TIniFile;
i,j:Integer;
st,value:TStringList;
tmpNode,child:TTreeNode;
tmpstr:String;
begin
a:=TIniFile.Create(文件名称);
st:=TStringList.Create;
value:=TStringList.Create;
a.ReadSections(st);
for i:=0 to st.Count-1 do
begin
tmpNode:=TreeView1.Items.Add(nil,st.Strings[i]);
a.ReadSection(st.Strings[i],value);
for j:=0 to value.Count-1 do
begin
tmpStr:=a.ReadString(st.Strings[i],value.Strings[j],'');
Child:=TreeView1.Items.AddChild(tmpNode,value.Strings[j]+'='+tmpstr);
end;
end;
value.Free;
st.Free;
a.Free;
end;
程序uses IniFilesprocedure TForm1.Button1Click(Sender: TObject);
var MyIniFile:TInifile;
StrSections,StrSection:TStringList;
i_Loop,j_Loop:Integer;
ParentNode,ChildNode:TTreeNode;
ParentCaption,ChildCaption:String;
ParentVal,ChildVal:^String;
Flag:integer;
begin
TreeView1.Items.Clear;
Flag:=0;
Try
MyIniFile:=TIniFile.Create('d:\test.ini');
StrSections:=TStringList.Create;
MyIniFile.ReadSections(StrSections);
for i_Loop := 0 to StrSections.Count - 1 do
begin
Flag:=1;
ParentCaption:=StrSections.Strings[i_Loop];
Try
StrSection:=TStringList.Create;
MyIniFile.ReadSection(ParentCaption,StrSection);
New(ParentVal);
ParentVal^:='这是段,有' + IntToStr(StrSection.Count) + '个变量';
ParentNode:=TreeView1.Items.Add(nil,'ParentCaption');
ParentNode.Text:=ParentCaption;
ParentNode.Data:=ParentVal;
for j_Loop := 0 to StrSection.Count - 1 do
begin
Flag:=2;
ChildCaption:=StrSection.Strings[j_Loop];
New(ChildVal);
ChildVal^:=MyIniFile.ReadString(StrSections[i_Loop],StrSection[j_Loop],'');
ChildNode:=TreeView1.Items.AddChild(ParentNode,ChildCaption);
ChildNode.Data:=ChildVal;
end;
Finally
StrSection.Free;
end;
end;
Finally
if Flag > 1 then
begin
Dispose(ParentVal);
Dispose(ChildVal);
end
else if Flag>0 then
Dispose(ParentVal); StrSections.Free;
MyIniFile.Free;
end;
end;procedure TForm1.TreeView1Click(Sender: TObject);
var MyTreeNode:TTreeNode;
sVal:^String;
begin
if (TreeView1.Items.Count<=0) or (TreeView1.Selected=nil) then
Exit;
MyTreeNode:=TreeView1.Selected;
sVal:=MyTreeNode.Data;
Edit1.Text:=sVal^;
end;
窗体上添加一个Button1,一个Edit1,一个TreeView1;
程序uses IniFilesprocedure TForm1.Button1Click(Sender: TObject);
var MyIniFile:TInifile;
StrSections,StrSection:TStringList;
i_Loop,j_Loop:Integer;
ParentNode,ChildNode:TTreeNode;
ParentCaption,ChildCaption:String;
ParentVal,ChildVal:^String;
Flag:integer;
begin
TreeView1.Items.Clear;
Flag:=0;
Try
MyIniFile:=TIniFile.Create('d:\test.ini');
StrSections:=TStringList.Create;
MyIniFile.ReadSections(StrSections);
for i_Loop := 0 to StrSections.Count - 1 do
begin
Flag:=1;
ParentCaption:=StrSections.Strings[i_Loop];
Try
StrSection:=TStringList.Create;
MyIniFile.ReadSection(ParentCaption,StrSection);
New(ParentVal);
ParentVal^:='这是段,有' + IntToStr(StrSection.Count) + '个变量';
ParentNode:=TreeView1.Items.Add(nil,'ParentCaption');
ParentNode.Text:=ParentCaption;
ParentNode.Data:=ParentVal;
for j_Loop := 0 to StrSection.Count - 1 do
begin
Flag:=2;
ChildCaption:=StrSection.Strings[j_Loop];
New(ChildVal);
ChildVal^:=MyIniFile.ReadString(StrSections[i_Loop],StrSection[j_Loop],'');
ChildNode:=TreeView1.Items.AddChild(ParentNode,ChildCaption);
ChildNode.Data:=ChildVal;
end;
Finally
StrSection.Free;
end;
end;
Finally
if Flag > 1 then
begin
Dispose(ParentVal);
Dispose(ChildVal);
end
else if Flag>0 then
Dispose(ParentVal); StrSections.Free;
MyIniFile.Free;
end;
end;procedure TForm1.TreeView1Click(Sender: TObject);
var MyTreeNode:TTreeNode;
sVal:^String;
begin
if (TreeView1.Items.Count<=0) or (TreeView1.Selected=nil) then
Exit;
MyTreeNode:=TreeView1.Selected;
sVal:=MyTreeNode.Data;
Edit1.Text:=sVal^;
end;