关闭mdi子窗体会导致System.ObjectDisposedException

时间:2017-10-05 00:04:16

标签: vb.net winforms mdi

我创建了一个mdi数据库应用程序。一个子表单具有datagridview。单击该表单上的按钮时,将打开一个新表单,其中包含pdfviewer(AxAcroPDF1)。此表单显示存储在sql server数据库中的pdf文档。

关闭此表单由Me.Close()完成。它会关闭它,但当我多次这样做时,打开那个表格并关闭它,然后在某一点上我得到一个 System.ObjectDisposedException,表示无法访问已处置的对象,并且未在try catch块中捕获。

它还说应用程序处于中断模式。您的应用已进入中断状态,但没有要显示的代码,因为执行外部代码的所有线程(通常是系统或框架代码)

之前从未遇到过这种问题。

以下是使用pdf viewer

启动表单的代码
Private Sub btnShowDocument_Click(sender As Object, e As EventArgs) Handles btnShowDocument.Click
    frmDocument = New frmShowDocumentatie

    rowIndex = dgvData.CurrentRow.Index

    FileName = dgvData.Item(6, rowIndex).Value


        If FileName = "" Then
            MessageBox.Show("Can't open documentation" & vbNewLine & "File doesn't exist", "Database Info", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Else
             frmDocument.Show()

        End If      

End Sub

这就是打开的形式

Imports System.Data.SqlClient

Public Class frmShowDocumentatie


'local variables to get passed the public shared values from
'curent selected row index and document name in datagridview
Private iRow As Integer
Private fName As String

Private Sub frmShowDocumentatie_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'sets the mdi parent
    MdiParent = MDIParent1
    'sets the text of the form to the filename of pdf bestand
    Me.Text = "Document: " & getFilename()

    'loads the pdf bestand, need the filepath which is provided by getfilepath function
    AxAcroPDF1.src = GetFilePath()

    'gets the values from the selected row index and corrsponding filename
    iRow = frmDataView.rowIndex
    fName = frmDataView.FileName

    tsmiDocument.Text = "Document: " & getFilename()

End Sub

'function for getting the filename of selected pdf file in datagridview
Public Function getFilename() As String
    fName = frmDataView.FileName
    getFilename = fName
End Function

'gets the file path of selected pdf bestand
Function GetFilePath() As String
    'Dim i As Integer = frmVerledenOverzicht.dgvData.CurrentRow.Index
    'Dim filename As String = frmVerledenOverzicht.dgvData.Item(6, i).Value

    Dim sFilePath As String
    Dim buffer As Byte()
    Using conn As New SqlConnection("Server=.\SQLEXPRESS;Initial Catalog=AndriesKamminga;Integrated Security=True;Pooling=False")
        conn.Open()

        Using cmd As New SqlCommand("Select  Bestand From dbo.PaVerledenOverzicht WHERE Documentatie =" & "'" & getFilename() & "';", conn)
            buffer = cmd.ExecuteScalar()
        End Using
        conn.Close()
    End Using
    sFilePath = System.IO.Path.GetTempFileName()
    System.IO.File.Move(sFilePath, System.IO.Path.ChangeExtension(sFilePath, ".pdf"))
    sFilePath = System.IO.Path.ChangeExtension(sFilePath, ".pdf")
    System.IO.File.WriteAllBytes(sFilePath, buffer)

    'returns the file path needed for AxAcroPDF1
    GetFilePath = sFilePath

End Function

Private Sub tsmiSluiten_Click(sender As Object, e As EventArgs) Handles tsmiSluiten.Click
    Try
         Me.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message, "Database Info", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

End Class

任何人都知道导致此异常的原因以及为什么程序崩溃而不是陷入try catch块 我正在使用2017年视觉工作室社区版

0 个答案:

没有答案