Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SelectDirectory/ShBrowseForFolder issue when having a lot of shell items

I'm running into a very strange problem with the Vcl.FileCtrl function SelectDirectory (which is a thin wrapper around the ShBrowseForFolder Win32 API).

I'm using the following code to allow the user to browse for a file or a folder:

  if SelectDirectory('Sélectionnez un élément à ajouter :', '', S, [sdNewFolder,
    sdShowFiles, sdNewUI]) then

When executing this code, the "Browse for folder" dialog is correctly shown, displaying the content of the user's Desktop:

SelectDirectory with less than 100 desktop files

But when the number of items in the Desktop folder is excessively large (on my computer, I can reproduce the problem by having approx. 100 desktop icons), the same call produces a totally different display:

SelectDirectory with more than 100 desktop files

In this case, the Desktop items aren't shown anymore. I'm only allowed to explore my home folder and since I've lost the "My Computer" icon I cannot select a file/folder outside of this directory.

I'm searching what I'm missing here. Is there a limit in the number of subitems a root item can have for a correct display? Having a lot of desktop icons is certainely not a good practice, but as far as I know that shouldn't prevent this dialog to operate normally. I'm not sure if there is something wrong in the Delphi wrapper, or if that's a limitation of the API I didn't see on MSDN...

Any hint appreciated!

like image 585
Adrien Reboisson Avatar asked Mar 08 '13 12:03

Adrien Reboisson


1 Answers

Yes, I can reproduce this behaviour. It's clearly a Windows limitation and the API in question does not offer you any way to increase buffers. I think your chances of working around it using SHBrowseForFolder are close to zero. Because SHBrowseForFolder is now a a legacy API.

If you are browsing for folders then you should use IFileOpenDialog in folder selection mode. That's a a much nicer dialog that uses the new Vista dialogs. In Delphi that is wrapped by TFileOpenDialog. Only use that if Win32MajorVersion>=6 though! For XP you need to call back on SHBrowseForFolder.

if Win32MajorVersion>=6 then
begin
  FileOpenDialog1.Title := 'Sélectionnez un élément à ajouter :';
  FileOpenDialog1.Options := FileOpenDialog1.Options + [fdoPickFolders];
  if FileOpenDialog1.Execute then
    Beep;
end else
begin
  // revert to SelectDirectory
end;
like image 162
David Heffernan Avatar answered Oct 12 '22 23:10

David Heffernan