主要是关于小节删除的问题
[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]

解决方案 »

  1.   

    [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=^^^^^^
    改后
    [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=^^^^^^
      

  2.   


    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;
      

  3.   

    由于小节很多,不可能一一写帮我看下什么地方错了
    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;
      

  4.   

    错误提示list index of bounds(-1)
      

  5.   

    可能是删光了还继续删估计出错的位置在
              n:=List.IndexOf('[frm+IntToStr(j+1)]');
              List[n]:='[frm+IntToStr(j)]';
    你断点跟踪一下,ini中内容先别那么多
      

  6.   

    象这种改名,建议重新生成,否则直接在同一文件上面操作,就会有一个逻辑顺序问题,比如说fram4先改成了fram3,那么就会存在两个fram3,再将fram3改为fram2的话,如果之前的fram3和fram4在文件当中是有序的,倒还好,如果不是的话,那就麻烦了,很有可能最终的结果导致的是fram4改为了fram2,而fram3却没有变。
      

  7.   

    procedure DeleteIniIndex(ini: TIniFile;Index: Integer);
    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;
      

  8.   

    procedure TForm1.Button5Click(Sender: TObject);     //刪除窗體
    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;
    这是我自己的方法,虽然麻烦了点,但是还是解决问题了,因为才学几天,掌握的东西就这么点,我只能在自己了解的范围类用自己的方法解决!觉得编程最重要的是思想。