Directory Dialog



Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here



ActiveX Control

Directory Dialog is another ActiveX control that allows developers to have easy access to the common Windows Directory Dialog.  Alternatively, you could write the API commands into your program, but if you use this control in a lot of your programs, you will save space by not having the same code in all your programs.  There is only one method in this ActiveX control, which is .DirDlg.  You can download DirDlg here or read its text file here; you can also download DirDlg2 here or read its HTML file here.  I will explain the difference later.


Here is an example of the control being used:

The Directory Dialog Control
The Directory Dialog Control

The code for doing this is simple:

Dim var As String
var = DirDlg1.DirDlg(Me, "Choose a folder:", 17)
If var = "" Then Exit Sub
Text1.Text = var

When the user chooses a folder and presses OK, the String will go into the 'var' variable.  If the Cancel button is pressed, the method will return an empty string and the process will exit.  Text1's .Text property is then passed the 'var' string.  You write the string that will appear above the list of folders and the flag after the name of the owner form.  To change the item at the top of the folder list, you change the flag.  Here are a list of flags known to me:

Flag What is Displayed
0 Desktop
1 Internet Explorer
2 Programs
3 Control Panel
4 Printers
5 My Documents
6 Favorites
7 StartUp
8 Recent
9 SendTo
10 Recycle Bin
11 Start Menu
12-15 <Illegal Operation>
17 My Computer
18 My Network Places 
19 NetHood
21 ShellNew
22 C:\WINDOWS\All Users\Start Menu
23 C:\WINDOWS\All Users\Start Menu\Programs
24 C:\WINDOWS\All Users\Start Menu\Programs\StartUp
25 C:\WINDOWS\All Users\Desktop
26 C:\WINDOWS\All Users\Application Data
27 C:\WINDOWS\PrintHood
28,29 <Illegal Operation>
30 StartUp
31 C:\WINDOWS\All Users\Favorites
32 Temporary Internet Files
33 Cookies
34 History
>=35 <Illegal Operation>

If you know any other flags or you have an explanation for the 'Illegal Operations', contact me.  As you can see, the above example used 17 - My Computer.

There is a difference between DirDlg and DirDlg2.  With DirDlg, the code for doing the same as above is this:

Dim var As String
Me.Enabled = False
var = DirDlg1.DirDlg("Choose a folder:", 17)
Me.Enabled = True
If var = "" Then Exit Sub
Text1.Text = var

The difference is that DirDlg does not have any support for an owner form, so the form has to be disabled manually.

Archived Comments

From: branty
Date: Tuesday, June 7, 2005 at 23:54:00
Comments: This tool looks perfect for what I'm trying to do so I downloaded it and tried to get it to work - now maybe I'm just too much of a newbie but I'm really struggling. I've included the OCX file in my components but there I get stuck. I've tried adding the code to my main form where the command button is - errors. I've created a new form and added the code to the activate method - errors. I'm just not sure where o go from here. Can you give a bit more detail on just how to use this?

Thanks in advance.

Reply: You should be able to just put the control on the Form and use the above code in a Command Button's Click event.  What errors are you getting?
From: Sidoh
Date: Friday, August 27, 2004 at 03:47:53
Comments: I like your control and have found it usefull (because I'm lazy). However, here's a few suggestions (unless you have implemented them in a different way and I have not noticed:

Use a params keyword for flags (I think its ParamArray in VB) so you can have more than one flaga ta time.

From: Ruturaj
Date: Wednesday, February 25, 2004 at 20:15:45
Comments: How to add "Make new Folder" button this dialog box ?
Reply: I used the code from a project at VB Helper to create this control.  I am not sure how you would add a "Make new Folder" button, but it should be possible by altering the code somehow.  The project can be downloaded from here: (1.51 KB)

Let me know if you manage to do it.

From: Maverick
Date: Monday, February 3, 2003 at 09:33:31
Comments: What if I just wanted a Explorer view in a form?

The best way of doing this involves using the Microsoft Internet Controls (SHDOCVW.DLL) control with this code:

WebBrowser1.Navigate2 "c:\windows\desktop"

It will look like this and will allow you to open files the same as Windows Explorer:

An example of using the Microsoft Internet Controls control
An example of using the Microsoft Internet Controls control

Martin Allen 1999 - 2011.  Last updated Wednesday 10 August 2011 07:27:00 PM +0100.