OpenFileDialog - 检查是否未被选中

时间:2013-09-28 00:22:36

标签: vb.net

我正在开发一个从Excel打开文件的过程。我想插入一个支票,如果用户在没有选择文件的情况下按下“打开”,则会弹出一个消息框警告。

这是我想要插入支票的代码的一部分。我尝试使用Is Nothing,但它对我不起作用。

            If GetOpenFileName.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                fileStream = GetOpenFileName.OpenFile()

                If (fileStream Is Nothing) Then 'I tried checking here, but it does not fire.
                    vmbContinue = MsgBox(strAlert, MsgBoxStyle.RetryCancel + MsgBoxStyle.Critical, "No Workbook Seletected")

                    If vmbContinue = MsgBoxResult.Cancel Then
                        xlWB.Close(SaveChanges:=False)

                        Exit Sub

1 个答案:

答案 0 :(得分:1)

不知道你究竟要求的是什么,但这是我在打开文件时使用的代码,如果他们不选择一个,它就不会让它们。

Dim ofd As New OpenFileDialog
    With ofd
        'select the atributes before opening
        'the filename (will show in the filename box)
        .FileName = ""
        'the title of the window to open a file
        .Title = "Open File..."
        'the extension filter
        .Filter = "Exel Files|*.exel"
        ''now to open the dialogue and check if OK has been pressed
        If .ShowDialog = Windows.Forms.DialogResult.OK Then
            MsgBox("File Opened Sucessfully")
            'put the code here when they choose a file
        Else
            MsgBox("Please Select A File")
            'put the code here if they click cancel or close
        End If
    End With