Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to view/select Folder like CommonDialog in VB6.0

Posted on 2004-08-31
7
Medium Priority
?
3,992 Views
Last Modified: 2013-12-25
I would like to view or select folder like “CommonDialog”- Box ( That was for only for file) in VB 6.0 .
  “DirListBox” does not meet my user requirements,  They want to select Folder as the way they select file using “CommonDialog”- in vb 6.0.
can you help me on that…?
Plz Help me on that…
Thanks in Advance
0
Comment
Question by:muntasirrahman
  • 3
  • 3
7 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 11942977
I've had this same issue. YOu can show a directory selection list but I've never found a way of initialising this to the desired path.

I have my own form I have written with the following controls:
Buttons - cmdCancel and cmdOk
DriveListBox - Drive1
DirectoryListBox - Dir1


Option Explicit

Private sPath As String
Private sDrive As String
Public bCancel As Boolean

Private Sub cmdCancel_Click()

    sPath = ""
    Unload Me

End Sub

Private Sub cmdOk_Click()
   
    sPath = Dir1.Path
    bCancel = False
    Unload Me

End Sub


Private Sub Drive1_Change()

    On Error GoTo Trap
    Dir1.Path = Drive1.Drive
    GoTo SkipTrap
   
Trap:

If vbRetry = MsgBox("Error " & Err.Number & vbNewLine & Err.Description, vbRetryCancel Or vbCritical) Then
    Resume
Else
    Dir1.Path = ""
End If

SkipTrap:

End Sub


Public Property Get Path() As String
   
    Path = sPath
   
End Property


Public Property Let Path(Path As String)
   
    On Error Resume Next
    sPath = Path
    sDrive = Left$(sPath, 2)
    Drive1.Drive = sDrive
    With Dir1
        .Path = Drive1.Drive
        .Path = sPath
    End With
   
End Property


Private Sub Form_Load()
   
    bCancel = True
   
End Sub



The code to use the form is this

    Dim sPath As String
   
    Dim f As frmLocateDir
    Set f = New frmLocateDir
    With f
       .Path = "C:\" 'Initialise to whatever you want
       .Show vbModal
        If Not .bCancel Then
          sPath = .Path
        End If
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11943021
In a Module:
Option Explicit

Private Type BROWSEINFO
  hOwner As Long
  pidlRoot As Long
  pszDisplayName As String
  lpszTitle As String
  ulFlags As Long
  lpfn As Long
  lParam As Long
  iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
            "SHGetPathFromIDListA" (ByVal pidl As Long, _
            ByVal pszPath As String) As Long
           
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
            "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
            As Long
           
Private Const BIF_RETURNONLYFSDIRS = &H1

Public Function BrowseFolder(szDialogTitle As String) As String
  Dim x As Long, bi As BROWSEINFO, dwIList As Long
  Dim szPath As String, wPos As Integer
 
    With bi
        .hOwner = 0
        .lpszTitle = szDialogTitle
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With
   
    dwIList = SHBrowseForFolder(bi)
    szPath = Space$(512)
    x = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
   
    If x Then
        wPos = InStr(szPath, Chr(0))
        BrowseFolder = Left$(szPath, wPos - 1)
    Else
        BrowseFolder = ""
    End If
End Function


Using it from a Form:
Private Sub Command1_Click()
    Dim fileName As String
    fileName = BrowseFolder("Select a folder")
    If fileName <> "" Then
        MsgBox fileName
    End If
End Sub
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11944174
Idle_Mind,
Is there anyway you can initialise the browse so it starts of in a user specified directory and not always at "My Computer" level?
JR
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 11944549
Sadly, I am aware of no way to set an initial directory.  There are actually two dialogs, an "old style" and a "new style".  The newer dialog is more robust, allowing the user to created directories, drag and drop, etc...

It can be accessed by using the BIF_USENEWUI new flag:

    Private Const BIF_USENEWUI = &H40

    With bi
        .hOwner = 0
        .lpszTitle = szDialogTitle
        .ulFlags = BIF_RETURNONLYFSDIRS Or BIF_USENEWUI
    End With

The rest of the code remains the same.

For more info take a look here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shbrowseforfolder.asp

Idle_Mind
0
 

Author Comment

by:muntasirrahman
ID: 11974911
Hi
    It meets my requirements.
Thanks a  lot …
It would be better if I could select a folder by default(ie: \\Gpra740\d\msc) while run my program rater than My Document. is that possible ?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11975068
See my previous post.

Regards,

{pseudocode} / {Time} ± ¼*ƒ(Me.Thoughts÷3)^² = Idle »(°_°)« Mind


0
 
LVL 18

Expert Comment

by:JR2003
ID: 12100427
I know it's a bit late but this is definitely worth a look. It allows you to set the initial directory and use the standard folder dialog.

http://www.mvps.org/btmtz/browsdlg/
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question