Solved

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

Posted on 2008-06-26
16
3,160 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
Comment Utility
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
Comment Utility
Do you have access to the program files ?

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

Expert Comment

by:jpaulino
Comment Utility
It works fine for me!
0
 
LVL 2

Author Comment

by:kmoloney
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
jpaulino - yes a windows form application

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

Author Comment

by:kmoloney
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
How would you like it uploaded?  As a compiled executable?

0
 
LVL 2

Author Comment

by:kmoloney
Comment Utility
...or source code files?
0
 
LVL 2

Author Comment

by:kmoloney
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now