两个重点:1.opendialog只对指定格式的文件才打开有效;2.只对listbox中没有的文件名才有效。
procedure TForm1.Button1Click(Sender: TObject);
var
sourcefn,newfn:string;
i:integer;
begin
opendialog1.Filter :='jpg文件(*.jpg)|*jpg|bmp文件(*bmp)|*bmp|所有文件(*.*)|*.*';
opendialog1.FilterIndex :=3;
opendialog1.Execute ; sourceFN :=opendialog1.FileName ;
newFN:=extractfilename(sourceFn); if opendialog1.FileName='' then
exit;
if (ExtractFileExt(sourcefn)<>'.bmp')or(ExtractFileExt(sourcefn)<>'.jpg') then
begin
showmessage('程序只允许导入‘jpg’或‘bmp’文件!');
exit;
end
else
begin
for i:=0 to listbox1.Items.Count-1 do
if newFN=listbox1.Items.Strings[i] then//避免项目重复导入?
begin
showmessage('项目中已经有了'+newFN) ;
exit ;
end
else
listbox1.Items.Append (sourcefn) ;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
sourcefn,newfn:string;
i:integer;
begin
opendialog1.Filter :='jpg文件(*.jpg)|*jpg|bmp文件(*bmp)|*bmp|所有文件(*.*)|*.*';
opendialog1.FilterIndex :=3;
opendialog1.Execute ; sourceFN :=opendialog1.FileName ;
newFN:=extractfilename(sourceFn); if opendialog1.FileName='' then
exit;
if (ExtractFileExt(sourcefn)<>'.bmp')or(ExtractFileExt(sourcefn)<>'.jpg') then
begin
showmessage('程序只允许导入‘jpg’或‘bmp’文件!');
exit;
end
else
begin
for i:=0 to listbox1.Items.Count-1 do
if newFN=listbox1.Items.Strings[i] then//避免项目重复导入?
begin
showmessage('项目中已经有了'+newFN) ;
exit ;
end
else
listbox1.Items.Append (sourcefn) ;
end;
end;
因为前面有exit了
var
sourcefn,newfn:string;
i:integer;
begin
opendialog1.Filter :='jpg文件(*.jpg)|*jpg|bmp文件(*bmp)|*bmp|所有文件(*.*)|*.*';
opendialog1.FilterIndex :=3;
if not opendialog1.Execute then exit; sourceFN :=opendialog1.FileName ;
newFN:=extractfilename(sourceFn); if opendialog1.FileName='' then
exit;
if (ExtractFileExt(sourcefn)<>'.bmp')or(ExtractFileExt(sourcefn)<>'.jpg') then
showmessage('程序只允许导入‘jpg’或‘bmp’文件!')
else
begin
for i:=0 to listbox1.Items.Count-1 do
if newFN=listbox1.Items.Strings[i] then//避免项目重复导入?
showmessage('项目中已经有了'+newFN) ;
else
listbox1.Items.Append (sourcefn) ;
end;
end;