0

如何使用未过滤和多选的 opendialog 仅加载到 listview .mp3 文件?我正在使用这种方法:

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 文件中。我需要一个不使用过滤器的程序。谢谢!

4

1 回答 1

9

You had several issues in your code. the i variable is not initialized, you must check for the extension inside of the for loop, Also you are checking the extension of the file using the FileName[i] property which get the current selected file (only valid in not multi-select mode), so you are comparing a element (char) of this property against the .mp3 instead you must use the Files property.

Try this

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;
于 2013-06-24T06:45:25.303 回答