Solved

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

Posted on 2004-08-31
7
3,764 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Exit a vb6 apps when a calling it apps closes 15 63
Passing a Text Box name to a Sub 6 104
MS SQL Update query with connected table data 3 81
checkbox to hide entire section 10 76
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

752 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