主要是关于小节删除的问题
[code][language]
ivar=0
[frmCount]
wCount=4
[frm1]
total=5
frmCaption=test1
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
label5=^^^^^^
[frm2]
total=3
frmCaption=test2
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
[frm3]
total=4
frmCaption=test3
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
[frm4]
total=5
frmCaption=test4
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
label5=^^^^^^
[/code]
现在的问题是想把小节frm2删除,然后变成下面这样,也就是把frm3的小节名改成frm2,frm4改成frm3
[code][language]
ivar=0
[frmCount]
wCount=4
[frm1]
total=5
frmCaption=test1
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
label5=^^^^^^
[frm2]
total=3
frmCaption=test2
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
[frm3]
total=5
frmCaption=test4
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
label5=^^^^^^
[/code]
[code][language]
ivar=0
[frmCount]
wCount=4
[frm1]
total=5
frmCaption=test1
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
label5=^^^^^^
[frm2]
total=3
frmCaption=test2
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
[frm3]
total=4
frmCaption=test3
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
[frm4]
total=5
frmCaption=test4
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
label5=^^^^^^
[/code]
现在的问题是想把小节frm2删除,然后变成下面这样,也就是把frm3的小节名改成frm2,frm4改成frm3
[code][language]
ivar=0
[frmCount]
wCount=4
[frm1]
total=5
frmCaption=test1
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
label5=^^^^^^
[frm2]
total=3
frmCaption=test2
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
[frm3]
total=5
frmCaption=test4
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
label5=^^^^^^
[/code]
ivar=0
[frmCount]
wCount=4
[frm1]
total=5
frmCaption=test1
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
label5=^^^^^^
[frm2]
total=3
frmCaption=test2
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
[frm3]
total=4
frmCaption=test3
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
[frm4]
total=5
frmCaption=test4
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
label5=^^^^^^
改后
[language]
ivar=0
[frmCount]
wCount=4
[frm1]
total=5
frmCaption=test1
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
label5=^^^^^^
[frm2]
total=4
frmCaption=test3
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
[frm3]
total=5
frmCaption=test4
label1=^^^^^^
label2=^^^^^^
label3=^^^^^^
label4=^^^^^^
label5=^^^^^^
uses IniFiles;
procedure TForm1.Button1Click(Sender: TObject);
var
MyIni:TMemIniFile;
List:TStringList;
i,n:integer;
begin
MyIni:=TMemIniFile.Create('CSDN答题.ini');
List:=TStringList.Create; MyIni.EraseSection('frm2');
MyIni.GetStrings(List); n:=List.IndexOf('[frm3]');
List[n]:='[frm2]';
n:=List.IndexOf('[frm4]');
List[n]:='[frm3]';
MyIni.SetStrings(List);
MyIni.UpdateFile; List.Free;
MyIni.Free;
end;
procedure TForm1.Button5Click(Sender: TObject); //刪除窗體
var
i,j,m,n:Integer;
boxtext:string;
MyIni:TMemIniFile;
List:TStringList;
begin
for i := 1 to wCount do
begin
boxtext:=IniFile.ReadString('frm'+IntToStr(i),'frmCaption','');
if boxtext=Combobox1.Text then //如何根據Combobox的值來判斷小節
begin
if wCount>0 then
begin
MyIni:=TMemIniFile.Create(IniFile.FileName) ;
List:=TStringList.Create;
MyIni.GetStrings(List);
IniFile.EraseSection('frm'+IntToStr(i));
for j:= i to wCount-1 do
begin
n:=List.IndexOf('[frm+IntToStr(j+1)]');
List[n]:='[frm+IntToStr(j)]';
MyIni.SetStrings(List);
MyIni.UpdateFile;
end;
wCount:=IniFile.ReadInteger('frmCount','wCount',0);
wCount:= wCount-1;
IniFile.WriteInteger('frmCount','wCount',wCount );
Label3.Caption:= '窗體數量:' + IntToStr(wCount);
for m := 1 to wCount do
begin
Combobox1.Text:=IniFile.ReadString('frm'+IntToStr(1),'frmCaption','');
Combobox1.Items.Add(IniFile.ReadString('frm'+IntToStr(m),'frmCaption','')) ;
end;
end;
end;
end;
end;
n:=List.IndexOf('[frm+IntToStr(j+1)]');
List[n]:='[frm+IntToStr(j)]';
你断点跟踪一下,ini中内容先别那么多
var
SectionValues: TStringList;
LastIndex, Count: Integer;
I: Integer;begin
SectionValues := TStringList.Create;
try
Count := ini.ReadInteger('frmCount','wCount',0);
if Count < Index then Exit;
LastIndex := Index;
while LastIndex < Count do begin
ini.ReadSectionValues('frm' + IntToStr(LastIndex + 1), SectionValues);
for I := 0 to SectionValues.Count - 1 do begin
ini.WriteString('frm' + IntToStr(LastIndex),SectionValues.Names[I],SectionValues.Values[I]);
end;
Inc(LastIndex);
end;
ini.EraseSection('frm' + IntToStr(LastIndex));
Dec(Count);
ini.WriteInteger('frmCount','wCount',Count);
finally
SectionValues.Free;
end;
end;
var
i,j,m,n:Integer;
boxtext:string;
begin
for i := 1 to wCount do
begin
boxtext:= IniFile.ReadString('frm'+IntToStr(i),'frmCaption','');
wCount := IniFile.ReadInteger('frmCount','wCount',0 );
if boxtext=Combobox1.Text then //如何根據Combobox的值來判斷小節
begin
if wCount>0 then
begin
for j:= i to wCount do
begin
total:=IniFile.ReadInteger('frm'+IntToStr(j+1),'total',0);
IniFile.WriteInteger('frm'+IntToStr(j),'total',total);
frmCaption:=IniFile.ReadString('frm'+IntToStr(j+1),'frmCaption','');
IniFile.WriteString('frm'+IntToStr(j),'frmCaption',frmCaption);
for n := 1 to total do
begin
slabel:=IniFile.ReadString('frm'+IntToStr(j+1),'label'+IntToStr(n),'');
IniFile.WriteString('frm'+IntToStr(j),'label'+IntToStr(n),slabel);
end;
end;
wCount:= wCount-1;
IniFile.WriteInteger('frmCount','wCount',wCount );
Label3.Caption:= '窗體數量:' + IntToStr(wCount);
IniFile.EraseSection('frm'+IntToStr(wCount+1));
Combobox1.Text:= '';
ComboBox1.Items.Clear;
for m := 1 to wCount do
begin
Combobox1.Items.Add(IniFile.ReadString('frm'+IntToStr(m),'frmCaption','')) ;
Combobox1.Text:=IniFile.ReadString('frm'+IntToStr(1),'frmCaption','');
end;
Edit8.Text:= Combobox1.Text;
end;
end;
end;
end;
这是我自己的方法,虽然麻烦了点,但是还是解决问题了,因为才学几天,掌握的东西就这么点,我只能在自己了解的范围类用自己的方法解决!觉得编程最重要的是思想。