• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3569
  • Last Modified:

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

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
kmoloney
Asked:
kmoloney
  • 8
  • 5
  • 2
  • +1
1 Solution
 
Bruce_1975Commented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
Do you have access to the program files ?

theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.ProgramFiles
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
It works fine for me!
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
kmoloneyAuthor Commented:
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
 
Bruce_1975Commented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
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
 
kmoloneyAuthor Commented:
jpaulino - yes a windows form application

Bruce - will respond to your question in a few...
0
 
kmoloneyAuthor Commented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
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
 
kmoloneyAuthor Commented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
>> 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
 
kmoloneyAuthor Commented:
How would you like it uploaded?  As a compiled executable?

0
 
kmoloneyAuthor Commented:
...or source code files?
0
 
kmoloneyAuthor Commented:
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
 
kmoloneyAuthor Commented:
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
 
creepy42Commented:
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

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 8
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now