Solved

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

Posted on 2004-08-31
7
3,648 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 85

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 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 85

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now