Take the tour ×

When I am using "TBrowseForFolder" in Delphi XE with the options

object actBrowseCommonData: TBrowseForFolder
  Category = 'Base'
  Caption = 'Browse...'
  DialogCaption = 'Browse for Common data path'
  BrowseOptions = [bifUseNewUI]
  BeforeExecute = actBrowseCommonDataBeforeExecute
  OnAccept = actBrowseCommonDataAccept

in code, the event BeforeExecute has the following code:

  actBrowseProjectPath.Folder := eProjectPath.Text;   // "C:\test\"

Most of the time the dialog does not focus the selected path but only shows the list from the top. Some times (randomly?) though the selected row is visible. Why does not browse for folder show the selected path?

share|improve this question
which OS? - I'm pretty sure XP doesn't support showing the selected path. –  daven11 May 12 '11 at 10:54
I have found the following on Microsoft Connect: "When using BFFM_SETSELECTION and BIF_NEWDIALOGSTYLE in the code below. In XP and Vista this selects the folder and scrolls the list to make the selected folder visible to the user. However, in Windows 7 the list is not scrolled to show the selected folder" connect.microsoft.com/VisualStudio/feedback/details/518103/… Can it be related and or bring me closer to a solution? –  Tool May 12 '11 at 11:03
------------ --- –  Tool May 12 '11 at 11:18
@daven11, note that this bug only occurs on Windows 7 64-bit and Windows 2008 64-bit. –  Johan May 12 '11 at 21:01
add comment

1 Answer

up vote 2 down vote accepted

It seems to work with the example from Microsoft Connect:

uses ShlObj; //BFFM_INITIALIZED...

OnCallBack := actBrowseProjectPathCallback;

procedure TSettingsDialogBase.actBrowseProjectPathCallback(
  Sender: TBrowseForFolder; Wnd: HWND; uMsg: Cardinal; lParam, lpData: Integer;
  var Result: Integer);

  if uMsg = BFFM_INITIALIZED then
    SendMessage(Wnd, BFFM_SETSELECTION, 1, Integer(@Sender.Folder[1]));

This gives focus on the selected folder - even if it not was in the visible region before, in the tree. My problems seems to be solved! If someone see a problem with this - please respond!

Here's what the OP of that workaround had to say on:

I was able to find a workaround. The problem seems to be that when the BFFM_INITIALIZED callback occurs and you send the BFFM_SETSELECTION message, the tree must be open and initialized BEFORE the BFFM_SETSELECTION message is sent. In other words, it looks like the bug is that the scroll-to code is being executed before the tree is fully open and it fails.

If you send the BFFM_SETSELECTION message in the callback, the tree opens but the selection is not scrolled-to. To get it to scroll, you must send a SECOND identical BFFM_SETSELECTION message on a delay. THEN the tree scrolls to the selected folder. The first message in the BFFM_INITIALIZED callback still has to occur to open the tree, then a second delayed message must be sent and it will work. A couple of ways to do this. One is to set a flag when BFFM_INITIALIZED occurs and just continue sending the BFFM_SETSELECTION for every callback after that. Or you can set a timer in your code to fire the message a second time, say 500 to 1000 ms later.

share|improve this answer
+1 looks good –  Johan May 12 '11 at 21:00
add comment

Your Answer


