vb.net 取消在 OpenFileDialog 框中引发错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15296097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:40:55  来源:igfitidea点击:

cancel throws error in OpenFileDialog box

vb.netopenfiledialog

提问by ABANDOND ACOUNT

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.

我正在尝试实现一个 OpenFileDialog 框,它工作正常,除非我选择单击取消然后程序抛出错误,说找不到该文件,这让我感到困惑,因为我没有选择文件。

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

回答by Konrad Rudolph

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, TextBox3and Button2are neverappropriate names. Good identifiers increase the readability of your code tremendously.

您还应该考虑正确的变量名称。OpenFileDialog1TextBox3并且Button2从来没有合适的名称。好的标识符极大地提高了代码的可读性。

回答by Rok Jarc

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

Dialog 将在这两种情况下自行处理- 如果用户取消其预期操作,您将不做任何事情。这应该这样做:

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.

当然,您将不得不添加一些额外的错误处理,但那是另一回事了。

回答by Christian Sauer

        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

回答by WebLuke

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.

您需要将结果定义为 DialogResult 以检查它是否正常并将文件路径发送到您需要的任何地方。

回答by Dam50fifty

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.

在我的项目中,我使用了SaveFileDialog. 如果用户关闭了对话窗口,则没有文件名,并且会发生错误。使用下面的代码,除非有要使用的文件名,否则我的进程不会运行。

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 thento check if a filename has been saved, if not, don't run the code.

可以为OpenFileDialog. 只需添加一个if then以检查文件名是否已保存,如果没有,请不要运行代码。