如果不满足特定条件,如何停止加载表单?

时间:2017-06-08 22:07:41

标签: vb.net visual-studio

我在使用VB.NET,当表单加载时,它会检查文件是否存在。如果找到该文件,则继续加载,否则会出现错误框并退出。

这样的东西
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        Dim file As FileStream = New FileStream("target.txt", FileMode.Open, FileAccess.Read)

Catch ex As System.IO.FileNotFoundException
'code to stop form loading goes here.
End Sub
End Class

1 个答案:

答案 0 :(得分:1)

你的意思是这样吗?:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        Dim file As FileStream = New FileStream("target.txt", FileMode.Open, FileAccess.Read)
        'the rest of your "load" code goes here...
    Catch ex As System.IO.FileNotFoundException
        MessageBox.Show("File not found", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Close()
    End Try
End Sub

如果找不到文件,它将显示MessageBox并关闭表单。