一个有关全局的字符串列表的问题! ( 积分: 20 )

  • 主题发起人 主题发起人 dragon1983
  • 开始时间 开始时间
D

dragon1983

Unregistered / Unconfirmed
GUEST, unregistred user!
if OpenFile.Execute then begin<br> &nbsp; &nbsp;for i:=0 to OpenFile.Files.Count-1 do begin<br> &nbsp; &nbsp; &nbsp;Memo1.Lines.Add(ExtractFileName(OpenFile.Files.Strings));<br> &nbsp; &nbsp; { strList.Add(OpenFile.Files.Strings);//这句话有问题吗?}<br> &nbsp; &nbsp;end;<br> &nbsp;end else exit;<br>如上代码,如果把上句话加入到程序中去则Memo1中的内容就会全部一样,如下!<br>116.txt<br>116.txt<br>116.txt<br>116.txt<br>116.txt<br>116.txt<br>如果不要那句话则结果正常<br>111.txt<br>112.txt<br>113.txt<br>114.txt<br>115.txt<br>116.txt<br>求教各位大虾!
 
if OpenFile.Execute then begin<br> &nbsp; &nbsp;for i:=0 to OpenFile.Files.Count-1 do begin<br> &nbsp; &nbsp; &nbsp;Memo1.Lines.Add(ExtractFileName(OpenFile.Files.Strings));<br> &nbsp; &nbsp; { strList.Add(OpenFile.Files.Strings);//这句话有问题吗?}<br> &nbsp; &nbsp;end;<br> &nbsp;end else exit;<br>如上代码,如果把上句话加入到程序中去则Memo1中的内容就会全部一样,如下!<br>116.txt<br>116.txt<br>116.txt<br>116.txt<br>116.txt<br>116.txt<br>如果不要那句话则结果正常<br>111.txt<br>112.txt<br>113.txt<br>114.txt<br>115.txt<br>116.txt<br>求教各位大虾!
 
语句不全,不过这里看不出什么问题来,<br>检查一下你的程序,在其他地方Memo1 与strList 有什么关系?应该在其他地方有问题
 
procedure TfrmChangeFileName.BtnListClick(Sender: TObject);<br>var<br> &nbsp;i : integer;<br>begin<br> &nbsp;If FileList=nil then FileList := TStringList.Create else FileList.Clear;<br> &nbsp;MemoFileList.Lines.Clear;<br> &nbsp;OpenFile.FileName := '*.*';<br> &nbsp;OpenFile.Filter &nbsp; := '*.*';<br> &nbsp;if OpenFile.Execute then begin<br> &nbsp; &nbsp;for i:=0 to OpenFile.Files.Count-1 do begin<br> &nbsp; &nbsp; &nbsp;MemoFileList.Lines.Add(ExtractFileName(OpenFile.Files.Strings));<br> &nbsp; &nbsp; &nbsp;FileList.Add(OpenFile.Files.Strings); &nbsp;<br> &nbsp; &nbsp;end;<br> &nbsp;end else exit;<br> &nbsp;EditFileName.Text := ExtractFileName(OpenFile.Files.Strings[0]);<br> &nbsp;EditNumEnd.Text &nbsp; := inttostr((MemoFileList.Lines.Count-strtoint(EditNumStart.Text))-1);<br> &nbsp;if MemoFileList.Lines.Count&gt;=2 then begin<br> &nbsp; &nbsp;BtnUp.Visible &nbsp; := True;<br> &nbsp; &nbsp;BtnDown.Visible := True;<br> &nbsp;end;<br>end;
 
很健康的语句 光看这些看不出来 验证通过<br>procedure TForm1.Button1Click(Sender: TObject);<br>var i:integer;<br> &nbsp; &nbsp;strList:TStringList;<br>begin<br> strlist:=TStringList.Create;<br> if Form1.OpenDialog1.Execute then begin<br> &nbsp; &nbsp;for i:=0 to OpenDialog1.Files.Count-1 do begin<br> &nbsp; &nbsp; &nbsp;Memo1.Lines.Add(ExtractFileName(OpenDialog1.Files.Strings));<br> &nbsp; &nbsp; &nbsp;strList.Add(OpenDialog1.Files.Strings);<br> &nbsp; &nbsp; &nbsp;memo2.Lines.Add(StrList.Strings);<br> &nbsp; &nbsp;end;<br> &nbsp;end else exit;<br>end;
 
忘说了,我程序当中的var FileList : TStringList 是个全局变量,楼上的你再试试,我死活都搞不过来,快疯了,最后我想了一招绝的,问题是摆平了,但我想知道我先前的倒底问题出在哪里?自己改后的代码如下!<br>var<br> &nbsp;FileList : TStringList;<br>implementation<br>......<br><br>procedure TfrmChangeFileName.BtnListClick(Sender: TObject);<br>var<br> &nbsp;i : integer;<br>begin<br> &nbsp;If FileList=nil then FileList := TStringList.Create else FileList.Clear;<br> &nbsp;MemoFileList.Lines.Clear;<br> &nbsp;FileList.Clear;<br> &nbsp;OpenFile.FileName := '*.*';<br> &nbsp;OpenFile.Filter := '*.*';<br> &nbsp;if OpenFile.Execute then begin<br> &nbsp; &nbsp;for i:=0 to OpenFile.Files.Count-1 do begin<br> &nbsp; &nbsp; &nbsp;MemoFileList.Lines.Add(ExtractFileName(OpenFile.Files.Strings));<br> &nbsp; &nbsp; // FileList.Add(OpenFile.Files.Strings);//有了这一句上一句就不正常了<br> &nbsp; &nbsp;end;<br> &nbsp;end else exit;<br> &nbsp;EditFileName.Text := ExtractFileName(OpenFile.Files.Strings[0]);<br> &nbsp;for i:=0 to OpenFile.Files.Count-1 do begin &nbsp;//隔一条语句再给FileList赋值<br> &nbsp; &nbsp;FileList.Add(OpenFile.Files.Strings);<br> &nbsp;end;<br> &nbsp;EditNumEnd.Text := inttostr((MemoFileList.Lines.Count-strtoint(EditNumStart.Text))-1);<br> &nbsp;if MemoFileList.Lines.Count&gt;=2 then begin<br> &nbsp; &nbsp;BtnUp.Visible := True;<br> &nbsp; &nbsp;BtnDown.Visible := True;<br> &nbsp;end;<br>end;<br><br>大家帮忙看看!
 
没人知道吗?还是没人看贴啊!
 
不管出了什么问题,首先这样的效率就不高<br>为什么不做一个中间变量<br> &nbsp;S:= OpenFile.Files.Strings;<br> &nbsp;MemoFileList.Lines.Add(S);<br> &nbsp;FileList.Add(S);<br>...<br>String 是内联对象,这样的引用基本不消耗资源,至少不需要在两次的 Add 方法中重新计算Strings
 
同意楼上的!<br>我测试了一下你的代码!<br>用一个中间变量就可以解决问题了!!!!!!
 
接受答案了.
 
后退
顶部