Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling Cancels in Directory Dialog 1

Status
Not open for further replies.

Twullf

Mechanical
Jan 24, 2012
196
US
I have tried this in two different ways.
Code:
      Try
         Dim dialog As new FolderBrowserDialog()
         dialog.description = "Select Directory containing loft data." 
         strDir = dialog.ShowDialog()
         strDir = dialog.SelectedPath
      Catch E As Exception 
         MessageBox.Show("Program cannot continue without lofting data location.", "Error", _
               MessageBoxButtons.OK, MessageBoxIcon.Error)
         Exit Sub
      End Try

As well as
Code:
      Dim dialog As new FolderBrowserDialog()

      dialog.description = "Select Directory containing loft data." 
      strDir = dialog.ShowDialog()
      strDir = dialog.SelectedPath
      On Error GoTo DirCanceledError

.
.
.

      DirCanceledError:

         '------------------------------------------------------------------------------------------
         '   If the user cancels the Directory Form, Exit the program
         '------------------------------------------------------------------------------------------

            MessageBox.Show("Program cannot continue without lofting data location.", "Error", _
               MessageBoxButtons.OK, MessageBoxIcon.Error)

      Exit Sub

The first has no reaction when the cancel button is used and the second hits the error regardless of whether the cancel button is hit or not.

Any help with this would be greatly appreciated.
 
Replies continue below

Recommended for you

Check the dialog result and do something based on it. The example below simply exits if cancel is pressed.

Code:
Dim FolderBrowserDialog1 As New FolderBrowserDialog

' Then use the following code to create the Dialog window
' Change the .SelectedPath property to the default location
With FolderBrowserDialog1
    ' Desktop is the root folder in the dialog.
    .RootFolder = Environment.SpecialFolder.Desktop
    ' Select the C:\ directory on entry.
    .SelectedPath = "C:\"
    ' Prompt the user with a custom message.
    .Description = "Select the directory to export .dxf file(s)"
    [highlight]If .ShowDialog = DialogResult.OK Then
       strOutputFolder = .SelectedPath
   else
       exit sub
    End If[/highlight]
End With

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top