Solved

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

Posted on 2004-08-31
7
3,832 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 86

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 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

615 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