Results 1 to 11 of 11

Thread: Displaying Browse for Folder Dialog Box

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    10

    Talking Displaying Browse for Folder Dialog Box

    In order to display Browse for Folder dialog box using VB6, what object should i use? What is the method to display this dialog box when I click BROWSE... button?
    Please help me someone......

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Displaying Browse for Folder Dialog Box

    If you double-click on the picture of the folder, in your toolbox, you will get a DIR listbox

    Try this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Dir1_Click()
    4.   MsgBox Dir1.Path
    5. End Sub

  3. #3
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Displaying Browse for Folder Dialog Box

    There are two methods basically..

    API or the OCX, the Common Dialog OCX is in the control list you get when right clicking on your Toolbox and browsing for more. (Microsoft Common Dialog etc.etc)

    The API version can be found multiple times here on VBForums if you use search
    http://www.vbforums.com/search.php?

  4. #4
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Displaying Browse for Folder Dialog Box

    Use the CommonDialog control.

    If you don't see it in your toolbox, Click on Project, Components and set the Microsoft Common Dialog control.

    You open it with something like CommonDialog1.ShowOpen probably in a command button click event.

    There are quite a lot of things you can set before you open it. It's pretty well documented in VB6 help.

    Give it a try and post your code here if you have any problems.
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  5. #5
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Displaying Browse for Folder Dialog Box

    One thing about the Common Dialog which is not intuitive: If the user cancels without selecting a file, it can generate an error - so you do need an error trap.
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  6. #6
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Displaying Browse for Folder Dialog Box

    .CancelError = false

  7. #7
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Displaying Browse for Folder Dialog Box

    Here's an example. A bit rough, but it gives you the general idea.

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.   Dim DefaultSharedDBLocation As String
    4.   Dim ErrorMessage As String
    5.   Dim ErrCount As Integer
    6.  
    7.  
    8.   DefaultSharedDBLocation = App.Path
    9.   If Right(DefaultSharedDBLocation, 1) <> "\" Then
    10.     DefaultSharedDBLocation = DefaultSharedDBLocation & "\"
    11.   End If
    12.   DefaultSharedDBLocation = DefaultSharedDBLocation & "Workbook.xls"
    13.  
    14.   CommonDialog1.FileName = "*.xls" 'SharedDBLocation
    15.   CommonDialog1.DialogTitle = "My Dialog Title'"
    16.   ' Set CancelError is True
    17.   CommonDialog1.CancelError = True
    18.   On Error GoTo ErrHandler
    19.   ' Set flags
    20.   CommonDialog1.Flags = cdlOFNHideReadOnly
    21.   ' Set filters
    22.   CommonDialog1.Filter = "All Files (*.*)|*.*|Excel Files (*.xls)|*.xls"
    23.   ' Specify default filter
    24.   CommonDialog1.FilterIndex = 2
    25.   ' Display the Open dialog box
    26.   CommonDialog1.ShowOpen
    27.  
    28.   MsgBox "You selected " & CommonDialog1.FileName
    29.  
    30.  
    31. Exit Sub
    32.    
    33. ErrHandler:
    34.    
    35.   'Invalid file name
    36.   If Err = 20477 And ErrCount = 0 Then
    37.       'Use the default file name instead
    38.       CommonDialog1.FileName = DefaultSharedDBLocation
    39.       ErrCount = ErrCount + 1 'In case Default name is bad
    40.       Resume
    41.      
    42.   'User pressed quit
    43.   ElseIf Err = 32755 Then
    44.       Exit Sub
    45.      
    46.   'Other error
    47.   Else
    48.  
    49.     ErrorMessage = Err & " " & Error$
    50.     If MsgBox(ErrorMessage, vbRetryCancel) = vbRetry Then
    51.       Resume
    52.     Else
    53.       Exit Sub
    54.     End If
    55.  
    56.   End If
    57.  
    58.  
    59. End Sub
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  8. #8
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Displaying Browse for Folder Dialog Box

    Quote Originally Posted by Devion
    .CancelError = false

    Brian.BrainNotWorkingToday = True
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Displaying Browse for Folder Dialog Box



    Has someone helped you? Then you can Rate their helpful post.

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Displaying Browse for Folder Dialog Box

    The common dialog is a "Browse For File" control, not a "Browse For Folder" control. To folder browse, use this:
    VB Code:
    1. Public Function BrowseForFolder(hWndOwner As Long, sPrompt As String) As String
    2.  
    3.     Dim iNull As Integer
    4.     Dim lpIDList As Long
    5.     Dim lResult As Long
    6.     Dim sPath As String
    7.     Dim udtBI As BrowseInfo
    8.  
    9.     With udtBI
    10.         .hWndOwner = hWndOwner
    11.         .lpszTitle = lstrcat(sPrompt, "")
    12.         .ulFlags = BIF_RETURNONLYFSDIRS
    13.         .lpfnCallback = PassBackAddress(AddressOf BrowseCallBackProc)
    14.     End With
    15.  
    16.     lpIDList = SHBrowseForFolder(udtBI)
    17.     If lpIDList Then
    18.         sPath = String$(MAX_PATH, 0)
    19.         lResult = SHGetPathFromIDList(lpIDList, sPath)
    20.         Call CoTaskMemFree(lpIDList)
    21.         iNull = InStr(sPath, vbNullChar)
    22.         If iNull Then
    23.             sPath = Left$(sPath, iNull - 1)
    24.         End If
    25.     End If
    26.  
    27.     BrowseForFolder = sPath
    28.  
    29. End Function
    30.  
    31. Private Sub cmdBrowse_Click()
    32. Dim sDocFolderPath As String
    33. sDocFolderPath = BrowseForFolder(Form1.hWnd, "Select A Folder")
    34. If sDocFolderPath <> "" Then
    35.     'store selected folder in textbox
    36.     Text1.Text = sDocFolderPath
    37. End If
    38. End Sub

  11. #11
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    417

    Re: Displaying Browse for Folder Dialog Box

    Hi, you can also get the Broswe for folder using the Shell Object here a example if helps.

    VB Code:
    1. Private Sub CmdBrowse_Click()
    2. Dim oShell As Object, oFolder As Object
    3.  
    4.     Set oShell = CreateObject("Shell.Application")
    5.    
    6.     Set oFolder = oShell.BrowseForFolder(0, "Select a folder", 0)
    7.    
    8.     If Not oFolder Is Nothing Then
    9.         MsgBox oFolder.Items.Item.Path
    10.     End If
    11.    
    12.     Set oFolder = Nothing
    13.     Set oShell = Nothing
    14. End Sub
    When your dreams come true.
    On error resume pulling hair out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width