Get a Folder (Not Reg. new DLLs)

garakgarak Member Posts: 3,263
edited 2008-09-25 in NAV Tips & Tricks
When you need to browse for a folder (dialog) you can do this with using standard Windows tools :!:

Here an example:
Name	DataType	Subtype	Length
ShellControl	Automation	'Microsoft Shell Controls And Automation'.Shell	
Folder	Automation	'Microsoft Shell Controls And Automation'.Folder3	
FolderItems	Automation	'Microsoft Shell Controls And Automation'.FolderItems3	
FolderItem	Automation	'Microsoft Shell Controls And Automation'.FolderItem2	
Foldertxt	Text		1024

if isclear(ShellControl) then
  create(ShellControl);

Folder := ShellControl.BrowseForFolder(0,'Ordner suchen',0);
FolderItems := Folder.Items();
FolderItem := FolderItems.Item;
Foldertxt := format(FolderItem.Path);
clear(ShellControl);

Regards
Regards
Do you make it right, it works too!

Comments

  • Timo_LässerTimo_Lässer Member Posts: 481
    Thank your for this very usefull tip!
    I've immediatly added it to my "Tools Collection" ;-)
    Timo Lässer
    Microsoft Dynamics NAV Developer since 1997
    MSDynamics.de - German Microsoft Dynamics Community - member of [clip]
  • garakgarak Member Posts: 3,263
    Please. (Gern geschehen :-) )

    Where can i see the "Tools Collection" :?: Maybe there is some interesting things for me :mrgreen:
    Do you make it right, it works too!
  • Captain_DX4Captain_DX4 Member Posts: 230
    Be almost worth enhancing Codeunit 412 "Common Dialog Management" with this tidbit for system-wide usage. Great tip!
    Kristopher Webb
    Microsoft Dynamics NAV Developer
  • garakgarak Member Posts: 3,263
    Thanks.
    Do you make it right, it works too!
  • Chandan_kansalChandan_kansal Member Posts: 27
    Hello Garak
    I have tried the code given by u.But when I run the codeunit contained ur code then it is not running.It compiled fine...but run just for a moment.Can u give me the complete solution for it.
  • garakgarak Member Posts: 3,263
    Whats the problem? Let me see your source.
    Do you make it right, it works too!
  • markborgesmarkborges Member Posts: 136
    When using codeunit 412, be sure to deploy Comdlg.oca and Comdlg32.ocx along with client installations (and register it too).

    Unfortunatelly this DLL may not be present in the client Windows System folder and then your application will fail.

    :)
    Marcelo Borges
    New York, NY, U.S.A.
    Dynamics NAV Consultant
  • janpieterjanpieter Member Posts: 298
    Verry nice code. Ive been trying to achieve this for some time.

    In some situations however the browser window disappears to the background. A workarround for this is to provide the current window hWnd as the first parameter of the 'BrowseForFolder' function.

    But i can only obtain this hWnd by writing a DLL that uses API functions to get the hWnd of the current active window. Which brings us back to the root, we didn't want to distribute extra automation objects.

    Anyone got any code that obtains the hWnd of the current active window by only using excisting windows/internet explorer automation objects? :-s
    In a world without Borders or Fences, who needs Windows and Gates?
  • Timo_LässerTimo_Lässer Member Posts: 481
    janpieter wrote:
    [...]
    Anyone got any code that obtains the hWnd of the current active window by only using excisting windows/internet explorer automation objects? :-s

    Yes, here it is:

    ActiveWindow Automation 'CSideWindowCheck'.WindowCheck
    or (if you don't have that Automation)
    ActiveWindow Automation 'C/SIDE Utility Classes'.ActiveWindow
    IF ISCLEAR(ShellControl) THEN 
      CREATE(ShellControl); 
    IF ISCLEAR(ActiveWindow) THEN 
      CREATE(ActiveWindow);
    
    EXIT(FORMAT(ShellControl.BrowseForFolder(ActiveWindow.WindowHandle,Text000,0).Items().Item.Path));
    
    (I've optimized the code a little bit and it still works ;-) )
    Timo Lässer
    Microsoft Dynamics NAV Developer since 1997
    MSDynamics.de - German Microsoft Dynamics Community - member of [clip]
  • janpieterjanpieter Member Posts: 298
    hmm bunner i dont have the csidewindowcheck automation and the 'c/side utilit classes' is part of this download: http://www.mibuso.com/dlinfo.asp?FileID=169

    So my conclusion is you won't be able to do this properly without adding automation.

    Ehm Timo your code might look better this way but i think it will return an error if the user cancels the browse for folder window.

    The most efficient working code is in my opinion:
    CREATE(lclsShellControl);
    CREATE(lclsWindowMgt);
    lclsFolder := lclsShellControl.BrowseForFolder(lclsWindowMgt.hwndCurrForm, lText001, 0);
    // if user cancels the dialog box then lclsFolder will not be set
    IF NOT ISCLEAR(lclsFolder) THEN
    BEGIN
      Path := lclsFolder.Items().Item.Path;
      CLEAR(lclsFolder);
    END;
    CLEAR(lclsWindowMgt)
    CLEAR(lclsShellControl);
    

    (lclsWindowMgt is my own class you can use the one in the download above as well)
    In a world without Borders or Fences, who needs Windows and Gates?
  • DoomhammerDoomhammer Member Posts: 211
    hey, man, this is GREAT!!!! thanks a much. for this functionality, I am looking about five years \:D/
    Martin Bokůvka, AxiomProvis
  • PrebenRasmussenPrebenRasmussen Member Posts: 137
    Check the downloads for http://www.mibuso.com/dlinfo.asp?FileID=541 where you need ABSOLUTELY no automations AT ALL !

    (Guess who made it?)
  • janpieterjanpieter Member Posts: 298
    Futher i have been experimenting with this idea but is was still bothered by the fact that the directory picker wasnt running modal. But this morning i found a sollution to this by just adding a form wrapper and run the form invisible but modal. Just create a form like this example:
    global vars:
    Name	DataType	Subtype	Length
    gtPath	Text		250
    gtTitle	Text		250
    gbSuccess	Boolean		
    
    
    Form - OnOpenForm()
    CurrForm.VISIBLE := FALSE;
    SelectDirectory();
    CurrForm.CLOSE;
    
    SetParameters(Title : Text[250])
    gtTitle := Title;
    
    IsSuccessFull() : Boolean
    EXIT(gbSuccess);
    
    GetPath() : Text[250]
    EXIT(gtPath);
    
    
    
    SelectDirectory()
    
    Local vars
    lclsShellControl	Automation	'Microsoft Shell Controls And Automation'.Shell	
    lclsFolder	Automation	'Microsoft Shell Controls And Automation'.Folder3	
    lbSuccess	Boolean		
    Text001         TextVariable
    
    CREATE(lclsShellControl);
    
    IF (gtTitle = '') THEN
    BEGIN
      gtTitle := Text001;
    END;
    
    lclsFolder := lclsShellControl.BrowseForFolder(0, gtTitle, 0);
    gbSuccess  := NOT ISCLEAR(lclsFolder);
    
    IF gbSuccess THEN
    BEGIN
      gtPath := lclsFolder.Items().Item.Path;
      IF (COPYSTR(gtPath, STRLEN(gtPath), 1) <> '\') THEN
      BEGIN
        gtPath := gtPath + '\';
      END;
    
      CLEAR(lclsFolder);
    END;
    
    CLEAR(lclsShellControl);
    

    From your application add the following code to browse for a directory:
    Vars:
    YourPathVar : Text250
    
    lfrmSelectDirectory.SetParameters('Type a title here'); lfrmSelectDirectory.RUNMODAL();
    
    IF lfrmSelectDirectory.IsSuccessFull THEN 
    BEGIN
      YourPathVar := lfrmSelectDirectory.GetPath(); 
    END;
    
    [/i]
    In a world without Borders or Fences, who needs Windows and Gates?
  • ajhvdbajhvdb Member Posts: 672
    Never needed this.. until now, thx JanPieter
  • janpieterjanpieter Member Posts: 298
    ajhvdb wrote:
    Never needed this.. until now, thx JanPieter

    After more then a year :wink:

    Don't forget to thank everybody else.
    This topic is a good example where combined knowledge leads towards a sollution.
    In a world without Borders or Fences, who needs Windows and Gates?
  • wicwic Member Posts: 96
    hi dudes,
    if the code from GARAK doesnt suite you because of error when cancelling the window, just change it like:

    IF ISCLEAR(ShellControl_Au_Loc) THEN
    CREATE(ShellControl_Au_Loc);

    Folder_Au_Loc := ShellControl_Au_Loc.BrowseForFolder(0,'Chose Folder',0);
    :roll: IF ISCLEAR(Folder_Au_Loc) THEN
    :roll: EXIT('');
    FolderItems_Au_Loc := Folder_Au_Loc.Items();
    FolderItem_Au_Loc := FolderItems_Au_Loc.Item;
    FolderText_Te_Loc := FORMAT(FolderItem_Au_Loc.Path);
    EXIT(FolderText_Te_Loc);
    #### Only one can survive ######
  • ishwarsharma016ishwarsharma016 Member Posts: 50
    @garak Big thanks Garak :smile:
    Thanks,
    Ishwar Sharma

    My Blogs: Dynamics Community Blog | Blogspot
    Connect: Google + | Twitter
Sign In or Register to comment.