Visual Basic Help (FolderBrowserDialog)

Associate
Joined
4 Mar 2007
Posts
315
Location
United Kingdom
Ok well i am in the process of learning Visual Basic but have bumped into a small hurdle, primarily around this folderbrowserdialog

I want to be able to open the folder which ive selected in the browser dialogue when the button OK is pressed. Here is my current code.

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles materials.Click
        Dim Folder_Browse_Materials As New FolderBrowserDialog

        With Folder_Browse_Materials
            .RootFolder = Environment.SpecialFolder.Desktop

            .SelectedPath = path_name

            .Description = "Select Source Dir"
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
' THIS IS WHERE THE OPEN THE FOLDER SCRIPT SHOULD GO RIGHT? '
            End If
        End With

    End Sub

I tried using Shell on the defined Dims and Consts As the general idea of the script was to just test around file navigation on my system for instance the path_name was a public const i had defined which had a route of "C:\Windows" now i know if i were to shell like shell("explorer C:\Windows") it would open that directory, but i want to do it with a public const that i had defined or even a dim value as string for instance.
So how would i go about the first step of opening the file that has been selected?
Thanks.
 
Logic looks a bit screwed to be honest.

Looks like your code is saying.

If button1 is clicked, then create a new BrowserDialog, and "With" that, set up a few attributes AND if the OK button is pressed, then do something (like the Shell command).

BUT, the DialogResult isn't going to be "OK" during the "With" part.

Personally I'd do the following:

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles materials.Click
        Dim Folder_Browse_Materials As New FolderBrowserDialog
 
        With Folder_Browse_Materials
            .RootFolder = Environment.SpecialFolder.Desktop
 
            .SelectedPath = path_name
 
            .Description = "Select Source Dir"
 
        End With
 
Dim vbResult As DialogResult = Folder_Browse_Materials.ShowDialog
If vbResult=Windows.Forms.DialogResult.OK Then
 
End If
 
 
    End Sub

NOTE: My wording may not be exactly 100% correct, could be vbDialogResult or just DialogResult, but hopefully you'll get the general idea.
 
hrm seems like a better use, although how would i go about executing the selected directory using this method?
I understand the dialogresult is that of the selected folder in the browser but if i were to simply do something like msgbox(dialogresult) the result is 1 (guessing as its in an if) so how would i go about open up the dialogresult in a shell window?
 
No, the "DialogResult" is actually a value that is returned based on the BrowserDialog (OK, yes, no, cancel, etc), it's similar to the usual MsgBox result(s).

You're already setting the "SelectedPath" of the BrowserDialog within the "With" statement.

As soon as the ".ShowDialog" event is called, there's no further execution to your program until you hit the "OK" or "Cancel" button in which case your code then continues and you act upon the DialogResult whereby you simply get the new "SelectedPath" (if the DialogResult is 'OK') and take it from there.

*edit*

Something like the following (again, not 100% word for word as I've not got my VS IDE open)

Code:
Dim vbResult As DialogResult = Folder_Browse_Materials.ShowDialog
If vbResult=Windows.Forms.DialogResult.OK Then
   shell("explorer " & Folder_Browse_Materials.ShowDialog.SelectedFolder.ToString)
End If
 
Last edited:
Back
Top Bottom