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

Vb.net 如果不满足特定条件,如何停止加载表单?,vb.net,visual-studio,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, FileA

我在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

你是说像这样吗

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

它将显示一个
消息框
,如果找不到文件,则关闭表单。

要检查文件是否存在,应使用
if IO.file.exists(“target.txt”)
,如@jonathana的回答所述。要关闭表单,只需调用
Me.close()
。但是,如果此表单是由其他表单/方法打开的,则应在加载表单之前检查文件是否存在,如果存在,则继续加载表单;如果没有,什么也不做。