|
-
Jul 31st, 2002, 08:20 AM
#1
Thread Starter
Addicted Member
[resolved]selecting directory (commondialog thingy)
um i know how to select a file by adding the commondialog component, is selecting a directory similar? and can some tell me how ? thanks.
Last edited by Jack Daniels; Jul 31st, 2002 at 08:36 AM.
-
Jul 31st, 2002, 08:28 AM
#2
-= B u g S l a y e r =-
VB Code:
Option Explicit
Public Type BrowseInfo
hwndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Public Const BIF_RETURNONLYFSDIRS = 1
Public Const BIF_NEWDIALOGSTYLE = &H40
Public Const MAX_PATH = 260
Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Public Function BrowseForFolder(hwndOwner As Long, sPrompt As String) As String
'declare variables to be used
Dim iNull As Integer
Dim lpIDList As Long
Dim lResult As Long
Dim sPath As String
Dim udtBI As BrowseInfo
'initialise variables
With udtBI
.hwndOwner = hwndOwner
.lpszTitle = lstrcat(sPrompt, "")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE
End With
'Call the browse for folder API
lpIDList = SHBrowseForFolder(udtBI)
'get the resulting string path
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
'If cancel was pressed, sPath = ""
BrowseForFolder = sPath
End Function
'usage :
Private Sub Form_Click()
MsgBox BrowseForFolder(Me.hWnd, "select folder")
End Sub
should do what you want
-
Jul 31st, 2002, 08:34 AM
#3
Frenzied Member
If you want the user to select a file using the CommonDialog control add this code to your project and add a commonDialog control to your form
VB Code:
Private Function ShowFileDialog() As String
With CommonDialog1
.CancelError = True
.DialogTitle = "Select Datasource"
.Filter = "Microsoft Access Databases(*.mdb)|*.mdb"
.InitDir = App.Path
On Error GoTo cancel_error 'Need this in case user hits Cancel button!
.ShowOpen
ShowFileDialog = .fileName
End With
Exit Sub
cancel_error:
If Err.Number <> cdlCancel Then
'an error occured somewhere
MsgBox "Error #" & err.Number & vbcrlf & err.Description
End If
ShowFileDialog = "" 'Return an empty string
End Function
If, on the other hand, you want to allow the user to choose a folder you can do this.
Add this to a module
VB Code:
Option Explicit
Public Type BrowseInfo
hwndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Public Const BIF_RETURNONLYFSDIRS = 1
Public Const BIF_NEWDIALOGSTYLE = &H40
Public Const MAX_PATH = 260
Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Public Function BrowseForFolder(hwndOwner As Long, sPrompt As String) As String
'declare variables to be used
Dim iNull As Integer
Dim lpIDList As Long
Dim lResult As Long
Dim sPath As String
Dim udtBI As BrowseInfo
'initialise variables
With udtBI
.hwndOwner = hwndOwner
.lpszTitle = lstrcat(sPrompt, "")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE
End With
'Call the browse for folder API
lpIDList = SHBrowseForFolder(udtBI)
'get the resulting string path
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
'If cancel was pressed, sPath = ""
BrowseForFolder = sPath
End Function
and use it like this
VB Code:
Private Sub Command1_Click()
MsgBox BrowseForFolder(Me.hWnd, "select folder")
End Sub
-
Jul 31st, 2002, 08:35 AM
#4
Thread Starter
Addicted Member
Thanks :-) im shocking with API's, atleast XP does something now lol.
-
Jul 31st, 2002, 08:35 AM
#5
Frenzied Member
Damn you peet I copied that code from one of your posts LOL and then YOU beat me to it
-
Jul 31st, 2002, 02:57 PM
#6
-= B u g S l a y e r =-
Originally posted by ae_jester
Damn you peet I copied that code from one of your posts LOL and then YOU beat me to it
hehe... I didn't have to search for my post..
-
Aug 6th, 2002, 12:07 PM
#7
Addicted Member
I am using the nice code from Peet, my question:
How start with a specific folder instead of the root (C:\) ??
-
Jul 26th, 2009, 09:35 PM
#8
New Member
Re: [resolved]selecting directory (commondialog thingy)
Wow! This code work great! Thanks
-
Jul 26th, 2009, 09:44 PM
#9
New Member
Re: [resolved]selecting directory (commondialog thingy)
Wow! This code work great! Thanks
-
Jul 26th, 2009, 09:54 PM
#10
Re: [resolved]selecting directory (commondialog thingy)
Thanks for letting us know that you have your answer. Are you aware that if you have JavaScript enabled you can do that easily by pulling down the Thread Tools menu and selecting the Mark Thread Resolved item? Also if someone has been particularly helpful you have the ability to affect their forum "reputation" by rating their post. Only those ratings that you give after you have 20 posts will actually count, but in all cases the person you rate will see your rating and know that you appreciate their help.
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
|