Search code examples
vb.netfolderbrowserdialog

FolderBrowserDialog open again on cancel


When i open the FolderBrowserDialog then click cancel it reopens again. But, On the second FolderBrowserDialog when you click cancel again it properly closes. And when you select a path on the second FolderBrowserDialog, It does or returns nothing Can i stop the second FolderBrowserDialog on appearing when i click on cancel on the first? I dont know why is it appearing. thanks in advance.

here is my code:

Dim apppath
Try
    FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop
    FolderBrowserDialog1.SelectedPath = "C:\"
    FolderBrowserDialog1.Description = "Select File Location Path"
    If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        apppath = FolderBrowserDialog1.SelectedPath
    ElseIf FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel    Then
        Exit Sub
    End If
    My.Computer.FileSystem.WriteAllText(apppath & "apppath.txt", apppath, False)
    MessageBox.Show(apppath)
Catch ex As Exception
    MessageBox.Show("Invalid Location")
    Exit Sub

End Try


Solution

  • Try something like this

    Dim result as Windows.Forms.DialogResult = FolderBrowserDialog1.ShowDialog()
    If result = Windows.Forms.DialogResult.OK Then
        apppath = FolderBrowserDialog1.SelectedPath
    ElseIf result = Windows.Forms.DialogResult.Cancel    Then
        Exit Sub
    End If