如何使用未经过滤和多次选择的opendialog将.mp3文件加载到listview?我正在使用这种方法:
procedure TForm1.PlayClick(Sender: TObject);
var i:integer;
begin
if opendialog1.execute then
begin
if ExtractFileExt(opendialog1.FileName[i]) ='.mp3' then
begin
for I := 0 to opendialog1.files.Count - 1 do
begin
listview1.Items.Add.Caption:=extractfilename(opendialog1.Files[i]);
end;
end;
end else
begin
showmessage(opendialog1.Files[i]);
end;
end;
但我需要一个像这样的程序:
如果用户打开具有各种扩展名的文件夹,则opendialog只会添加到扩展名为.mp3的ListView文件中。我需要一个不使用过滤器的过程。谢谢!
答案 0 :(得分:9)
您的代码中存在多个问题。 i
变量未初始化,您必须检查for循环内部的扩展名,还要使用获取当前所选文件的FileName[i]
属性检查文件的扩展名(仅在不是多选模式),因此您要将此属性的元素(char)与.mp3
进行比较,而必须使用Files
属性。
试试这个
var
i:integer;
LItem : TListItem;
begin
if opendialog1.execute then
for i := 0 to OpenDialog1.Files.Count - 1 do
if SameText(ExtractFileExt(opendialog1.Files[i]), '.mp3') then
begin
LItem:=listview1.Items.Add;
LItem.Caption:=ExtractFileName(OpenDialog1.Files[i]);
end;
end;