Link to home
Start Free TrialLog in
Avatar of kmoloney
kmoloneyFlag for United States of America

asked on

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

Avatar of Bruce_1975
Bruce_1975

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

Avatar of Jorge Paulino
Do you have access to the program files ?

theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.ProgramFiles
It works fine for me!
Avatar of kmoloney

ASKER

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

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

jpaulino - yes a windows form application

Bruce - will respond to your question in a few...
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

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.
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."
>> 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 ?
How would you like it uploaded?  As a compiled executable?

...or source code files?
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

ASKER CERTIFIED SOLUTION
Avatar of kmoloney
kmoloney
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!