Solved

Folder Browser Dialog does not display dialog window, always returns "Cancel"

Posted on 2008-06-26
16
3,223 Views
Last Modified: 2013-11-26
I am trying to use the FolderBrowserDialog control in a simple windows forms application.  I only have two controls on a single form: txtFolder (textbox) and btnBrowseFolders (button).  Please see me code in the code snippet attached.

This code never displays a dialog box.  I just get a messagebox that shows "C:\Program Files" (because of the default SelectedPath value of SpecialDirectories.ProgramFiles).  Why isn't the dialog box appearing and allowing me to make a choice?

Thanks,

Kevin
Private Sub btnBrowseFolders_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseFolders.Click
 
        Dim theFolderBrowser As New FolderBrowserDialog
        theFolderBrowser.Description = "Please locate Directory..."
        theFolderBrowser.ShowNewFolderButton = False
        theFolderBrowser.RootFolder = Environment.SpecialFolder.Desktop
        theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.ProgramFiles
        If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.txtFolder.Text = theFolderBrowser.SelectedPath
        Else
            MessageBox.Show(theFolderBrowser.SelectedPath)
        End If

Open in new window

0
Comment
Question by:kmoloney
  • 8
  • 5
  • 2
  • +1
16 Comments
 
LVL 6

Expert Comment

by:Bruce_1975
ID: 21876035
Try this one. (I'm a C# guy, so there might be some compiler error...)

Regards,
Bruce
Private Sub btnBrowseFolders_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseFolders.Click
 
        Dim theFolderBrowser As New FolderBrowserDialog
        theFolderBrowser.Description = "Please locate Directory..."
        theFolderBrowser.ShowNewFolderButton = False
        ' don't use the root folder
        ' selected folder is a string, just set to empty
        theFolderBrowser.SelectedPath = ""
        If theFolderBrowser.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.txtFolder.Text = theFolderBrowser.SelectedPath
        Else
            MessageBox.Show(theFolderBrowser.SelectedPath)
        End If
End Sub

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21876068
Do you have access to the program files ?

theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.ProgramFiles
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21876073
It works fine for me!
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Author Comment

by:kmoloney
ID: 21876235
jpaulino (Yes, I have access - I'm an administrator).

Bruce - now all I get is an empty dialog box (no title, to text, just an OK and close button).

In debugging it skips through the If theFolderBrowser.ShowDialog=....Then
to the following else statement.  The value of the "theFolderBrowser.ShowDialog is 2, which is equivalent to Windows.Forms.DialogResult.Cancel, even though no dialog window appears.


    Private Sub btnBrowseFolders_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseFolders.Click
 
        Dim theFolderBrowser As New FolderBrowserDialog
        theFolderBrowser.Description = "Please locate Directory..."
        theFolderBrowser.ShowNewFolderButton = False
        theFolderBrowser.SelectedPath = ""
        If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.txtFolder.Text = theFolderBrowser.SelectedPath
        Else
            MessageBox.Show(theFolderBrowser.SelectedPath)
        End If
    End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:Bruce_1975
ID: 21876332
Sounds strange to me...
The empty dialog box might be the message box. Try to set the owner of the dialog

theFolderBrowser.ShowDialog(Me)

Is the form completely loaded?
Do you click on the button or do you call the function from somewhere else?
Is it the same for OpenFileDialog?

Regards,
Bruce
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21876511
Is is a windows form application that you're working on ?

Very stange behavier ...

Try this way:

	Dim theFolderBrowser As New FolderBrowserDialog
        theFolderBrowser.Description = "Please locate Directory..."
        theFolderBrowser.ShowNewFolderButton = False
        theFolderBrowser.RootFolder = Environment.SpecialFolder.Desktop
        theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.ProgramFiles
        Dim result As MsgBoxResult = theFolderBrowser.ShowDialog()
        If result = Windows.Forms.DialogResult.OK Then
            Me.txtFolder.Text = theFolderBrowser.SelectedPath
        Else
            MessageBox.Show(theFolderBrowser.SelectedPath)
        End If

Open in new window

0
 
LVL 2

Author Comment

by:kmoloney
ID: 21877343
jpaulino - yes a windows form application

Bruce - will respond to your question in a few...
0
 
LVL 2

Author Comment

by:kmoloney
ID: 21879729
The form is fully loaded.

I am calling the subroutine by clicking on the button.  I added a reference to the owner of the dialog (me).

The open file dialog does not work either!  I added an OpenFileDialog control (dlgOpenFile) and a button control (btnOpenFile).  See code below.  In response to clicking on btnOpenFile, I get no dialog, but a messagebox that says "No file selected".  The second messagebox returns "Cancel."

One other thing goofy...In the project properties, in the "Application" tab, Windows Application Framework Properties, I have to uncheck the "enable XP visual styles" to even get the form to load without error.  If the box is checked, I get a "Win32Exception was not handled" native error code 1400, "Windows class name is not valid" - Source: "System.Windows.Forms".


Public Class frmMain
 
    Private Sub btnBrowseFolders_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseFolders.Click
 
        Dim theFolderBrowser As New FolderBrowserDialog
        theFolderBrowser.Description = "Please locate Directory..."
        theFolderBrowser.ShowNewFolderButton = False
        theFolderBrowser.SelectedPath = ""
        If theFolderBrowser.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            Me.txtFolder.Text = theFolderBrowser.SelectedPath
        Else
            MessageBox.Show(theFolderBrowser.SelectedPath)
        End If
    End Sub
 
    Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click
        If Me.dlgOpenFile.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            MessageBox.Show(Me.dlgOpenFile.FileName.ToString)
        Else
            MessageBox.Show("No files selected.")
            MessageBox.Show(Me.dlgOpenFile.ShowDialog(Me).ToString)
 
        End If
    End Sub
End Class

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21879750
It seams the project has some problems ... have you consider create a new project and copy/paste the forms, classes and modules that you have into it and compile it again ? If the project is not to big it's quite easy to achive.

That code is pretty simple and work fine for me. The problems is on the solution and not in the code.
0
 
LVL 2

Author Comment

by:kmoloney
ID: 21879959
I started a new project, and got the same result.  Something buggy about the environment, I think.  The color dialog DOES work fine.  File and folder do not.  Could this be a permissions issue?  The help window on the FolderBrowserDialog states:

Security Note  
To use this class, your assembly requires a privilege level granted by the FileIOPermissionAttribute.PathDiscoveryProperty property, which is part of the FileIOPermissionAccess enumeration. If you are running in a partial-trust context, the process might throw an exception because of insufficient privileges. For more information, see Code Access Security Basics.
 

...and I have absolutely no idea what that means - except that I'm not getting an exception, I'm getting "Cancel."
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21879978
>> Could this be a permissions issue?

It doesn't seam to be . If it was it will throw an exception and you don't have it.

Can you do a sample project and upload it ?
0
 
LVL 2

Author Comment

by:kmoloney
ID: 21880181
How would you like it uploaded?  As a compiled executable?

0
 
LVL 2

Author Comment

by:kmoloney
ID: 21880191
...or source code files?
0
 
LVL 2

Author Comment

by:kmoloney
ID: 21880371
By the way, this is the error I receive when XP Visual Styles are enabled...
System.ComponentModel.Win32Exception was unhandled
  Message="Window class name is not valid."
  Source="System.Windows.Forms"
  ErrorCode=-2147467259
  NativeErrorCode=1400
  StackTrace:
       at System.Windows.Forms.NativeWindow.WindowClass.RegisterClass()
       at System.Windows.Forms.NativeWindow.WindowClass.Create(String className, Int32 classStyle)
       at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
       at System.Windows.Forms.Control.CreateHandle()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
       at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Control.set_Visible(Boolean value)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at TestDialog2.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Open in new window

0
 
LVL 2

Accepted Solution

by:
kmoloney earned 0 total points
ID: 21880734
I might have stumbled upon the problem...although I don't know exactly which it might have been.

http://forums.msdn.microsoft.com/en-US/netfxbcl/thread/8b861371-adb3-4f2d-94ca-df14d8f4516d

I uninstalled the Logitech app (actually, that plus the Logitech Desktop Manager and the Logitech Camera Driver).  I tried to see if that would repair the problem, before rebooting; it didn't.  So, also before rebooting, I ran devenv.exe /safemode, and it worked.  I did not remove any add-ins.  I then restarted Visual Studio in normal mode, and it still worked.

The addin I have is DevExpress Tools (DXCore extensibility layer for Visual Studio .NET, from Developer Express, Inc. (www.devexpress.com).  The add-in still works.

So I don't know if it was booting in safe mode and then rebooting in normal mode, or if it was removing the Logitech Camera settings, but I no longer get the error when XP Visual Styles are enabled, and both the FolderBrowserDialog and the OpenFileDialog now work.

Thanks for your help!
0
 

Expert Comment

by:creepy42
ID: 22315078
I have the same problem here, and I'm launching the folder dialog box in installer class, and I created a win form and I launched it and I set this form as an owner to folder dialog and nothing seemed to work!

this is very strange!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

786 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