0

I'm trying to implement an OpenFileDialog box, its works fine except if I choose to click cancel then program throws an error, saying that file can't be found, which confuses me cause I didnt select a file.

The following is the code. how I can implement the cancel button?

OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.FileName = "Select a Batch file..."
OpenFileDialog1.Filter = "Batch files (*.bat) | *.bat"
OpenFileDialog1.ShowDialog()

If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
    OpenFileDialog1.Dispose()
End If

Dim R As New IO.StreamReader(OpenFileDialog1.FileName)
TextBox4.Text = R.ReadToEnd
R.Close()

Button4.Enabled = True
Button6.Enabled = True
Ram
  • 3,092
  • 10
  • 40
  • 56
LabRat
  • 1,996
  • 11
  • 56
  • 91

5 Answers5

2

You commented out the (inadequate) handling of cancelling the dialog. Put it back in:

Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Batch files (*.bat)|*.bat|All files|*.*"
Dim result = openFileDialog1.ShowDialog()

If result = DialogResult.Cancel Then
    Return ' Just leave the method
End If

' … rest of method

You should also think about proper variable names. OpenFileDialog1, TextBox3 and Button2 are never appropriate names. Good identifiers increase the readability of your code tremendously.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    A minor correction Dim result As DialogResult = openFileDialog1.ShowDialog() – dbasnett Mar 08 '13 at 15:20
  • @dbasnett Unnecessary and not recommended with `Option Infer On`. – Konrad Rudolph Mar 08 '13 at 15:46
  • It is always better to specifically define the type, IMHO. – dbasnett Mar 08 '13 at 15:59
  • @dbasnett The type is irrelevant here, and inferred from context. Relying on `Option Infer` is a **good** thing to avoid redundancy. [Here’s an explanation](http://stackoverflow.com/a/41514/1968). And [Eric Lippert, former chief architect of C#, agrees](http://blogs.msdn.com/b/ericlippert/archive/2011/04/20/uses-and-misuses-of-implicit-typing.aspx). – Konrad Rudolph Mar 08 '13 at 16:01
  • I wonder if you would have had to correct Christian Sauer if he had explicitly defined 'result'. It is sloppy and prone to error IMHO, there are plenty of examples of why it isn't a good idea. I saw your opinion, "Now, the C# team has released a coding guideline stating that var should only be used to capture the result of a LINQ statement that creates an anonymous type... Well, screw that. As long as the C# team doesn't give me a sound argument for this guideline, I am going to ignore it because in my professional and personal opinion, it's pure baloney." Lets just disagree. – dbasnett Mar 08 '13 at 16:20
  • @dbasnett Well this discussion is unconstructive and while you’re of course entitled to your opinion you should be aware that you have the consensus of .NET experts against you. – Konrad Rudolph Mar 08 '13 at 16:37
2

Dialog will dispose itself in both cases - you simply don't do anything if user cancels his intended action. This should do it:

OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.FileName = "Select a Batch file..."
OpenFileDialog1.Filter = "Batch files (*.bat) | *.bat"
OpenFileDialog1.ShowDialog()

If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

   Dim R As New IO.StreamReader(OpenFileDialog1.FileName)
   TextBox4.Text = R.ReadToEnd
   R.Close()

   Button4.Enabled = True
   Button6.Enabled = True   

End If

Of course you will have to add some additional error handling but that is another story.

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
0
        Dim result = OpenFileDialog1.ShowDialog()

    If result = True Then
        Dim R As New IO.StreamReader(OpenFileDialog1.FileName)
        TextBox4.Text = R.ReadToEnd
       R.Close()

    Button4.Enabled = True
    Button6.Enabled = True
    else
        ' handle the error, e.g. msgbox (no vaild file chosen"
     End If
Christian Sauer
  • 10,351
  • 10
  • 53
  • 85
0

This is what worked for me for my project.

    Dim bResult As DialogResult = sfdReportFile.ShowDialog()

    If bResult = DialogResult.OK Then
        tbFilePathName.Text = sfdReportFile.FileName.ToString
    End If

You will need to define the result as a DialogResult to check if it was OK and send the file path to whatever you needed it for.

WebLuke
  • 156
  • 1
  • 5
0

On my project, I used the SaveFileDialog. If the user closed the dialog window, then there is no file name, and an error occurs. With my below code, my process wont run unless there is a file name to use.

If SaveFileDialog1.FileName = Nothing Then

Else
     Code to run here when a file name is selected.
End If

The same thing can be run for the OpenFileDialog. Just add an if then to check if a filename has been saved, if not, don't run the code.

Ram
  • 3,092
  • 10
  • 40
  • 56