|
-
Sep 1st, 2005, 03:41 AM
#1
Thread Starter
New Member
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......
-
Sep 1st, 2005, 03:57 AM
#2
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:
Option Explicit
Private Sub Dir1_Click()
MsgBox Dir1.Path
End Sub
-
Sep 1st, 2005, 03:58 AM
#3
Frenzied Member
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?
-
Sep 1st, 2005, 03:58 AM
#4
Fanatic Member
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)
-
Sep 1st, 2005, 04:01 AM
#5
Fanatic Member
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)
-
Sep 1st, 2005, 04:02 AM
#6
Frenzied Member
Re: Displaying Browse for Folder Dialog Box
.CancelError = false
-
Sep 1st, 2005, 04:06 AM
#7
Fanatic Member
Re: Displaying Browse for Folder Dialog Box
Here's an example. A bit rough, but it gives you the general idea.
VB Code:
Private Sub Command1_Click()
Dim DefaultSharedDBLocation As String
Dim ErrorMessage As String
Dim ErrCount As Integer
DefaultSharedDBLocation = App.Path
If Right(DefaultSharedDBLocation, 1) <> "\" Then
DefaultSharedDBLocation = DefaultSharedDBLocation & "\"
End If
DefaultSharedDBLocation = DefaultSharedDBLocation & "Workbook.xls"
CommonDialog1.FileName = "*.xls" 'SharedDBLocation
CommonDialog1.DialogTitle = "My Dialog Title'"
' Set CancelError is True
CommonDialog1.CancelError = True
On Error GoTo ErrHandler
' Set flags
CommonDialog1.Flags = cdlOFNHideReadOnly
' Set filters
CommonDialog1.Filter = "All Files (*.*)|*.*|Excel Files (*.xls)|*.xls"
' Specify default filter
CommonDialog1.FilterIndex = 2
' Display the Open dialog box
CommonDialog1.ShowOpen
MsgBox "You selected " & CommonDialog1.FileName
Exit Sub
ErrHandler:
'Invalid file name
If Err = 20477 And ErrCount = 0 Then
'Use the default file name instead
CommonDialog1.FileName = DefaultSharedDBLocation
ErrCount = ErrCount + 1 'In case Default name is bad
Resume
'User pressed quit
ElseIf Err = 32755 Then
Exit Sub
'Other error
Else
ErrorMessage = Err & " " & Error$
If MsgBox(ErrorMessage, vbRetryCancel) = vbRetry Then
Resume
Else
Exit Sub
End If
End If
End Sub
Brian
(Fighting with the RightToLeft bugs in VS 2005)
-
Sep 1st, 2005, 04:08 AM
#8
Fanatic Member
Re: Displaying Browse for Folder Dialog Box
 Originally Posted by Devion
.CancelError = false 
Brian.BrainNotWorkingToday = True
Brian
(Fighting with the RightToLeft bugs in VS 2005)
-
Sep 1st, 2005, 04:30 AM
#9
-
Sep 1st, 2005, 05:41 AM
#10
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:
Public Function BrowseForFolder(hWndOwner As Long, sPrompt As String) As String
Dim iNull As Integer
Dim lpIDList As Long
Dim lResult As Long
Dim sPath As String
Dim udtBI As BrowseInfo
With udtBI
.hWndOwner = hWndOwner
.lpszTitle = lstrcat(sPrompt, "")
.ulFlags = BIF_RETURNONLYFSDIRS
.lpfnCallback = PassBackAddress(AddressOf BrowseCallBackProc)
End With
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
lResult = SHGetPathFromIDList(lpIDList, sPath)
Call CoTaskMemFree(lpIDList)
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If
BrowseForFolder = sPath
End Function
Private Sub cmdBrowse_Click()
Dim sDocFolderPath As String
sDocFolderPath = BrowseForFolder(Form1.hWnd, "Select A Folder")
If sDocFolderPath <> "" Then
'store selected folder in textbox
Text1.Text = sDocFolderPath
End If
End Sub
-
Sep 1st, 2005, 06:11 AM
#11
Hyperactive Member
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:
Private Sub CmdBrowse_Click()
Dim oShell As Object, oFolder As Object
Set oShell = CreateObject("Shell.Application")
Set oFolder = oShell.BrowseForFolder(0, "Select a folder", 0)
If Not oFolder Is Nothing Then
MsgBox oFolder.Items.Item.Path
End If
Set oFolder = Nothing
Set oShell = Nothing
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|