我正在调用一个表单,我希望自动运行一些代码,然后再次关闭。在我成功运行后在表单上调用Close()
时,在调试时,它会将我踢回我运行表单的主应用程序并抛出以下异常:
System.ObjectDisposedException:'无法访问已处置的对象。 对象名称:' ShippingLabelPrintApp'。'
主要模块:
Module Main
Sub Main()
'Add save folder if it doesn't exist
If (Not System.IO.Directory.Exists(savePath)) Then
System.IO.Directory.CreateDirectory(savePath)
End If
'saveString = getImagePath()
'Establish whether we are auto or manual packing clamshells.
'packMode = ???
'If we are manually packing clamshells, send to the manual pack station shipping printer (ZEBRA)
'System.Windows.Forms.Application.Run(New ShippingLabelPrintApp)
Dim manualShipForm As New ShippingLabelPrintApp
manualShipForm.saveString = "\\Warhawk\users\username\Desktop\Label #794661031570.PNG"
manualShipForm.ShowDialog()
'If we are auto packing clamshells, send to the auto pack shipping printer (SATO)
'convertImage(saveString)
'transmitImage(saveString)
Environment.Exit(0)
End Sub
End Module
形式:
Imports System.Drawing.Printing
Public Class ShippingLabelPrintApp
Public saveString As String
Public Sub New()
' The Windows Forms Designer requires the following call.
InitializeComponent()
print()
End Sub
' Specifies what happens when the user clicks the Button.
Private Sub print()
Dim margins As New Margins(70, 0, 35, 0)
Dim paperSize As New PaperSize("Custom Paper Size", 400, 600)
Try
printDocument.PrinterSettings.PrinterName = "Manual Ship Printer"
'Set internally at the printer
'printDocument.DefaultPageSettings.PaperSize = paperSize
'printDocument.DefaultPageSettings.Margins = margins
printDocument.Print()
Catch ex As Exception
MsgBox("An error occurred while printing",
ex.ToString())
Finally
Close()
End Try
End Sub
' Specifies what happens when the PrintPage event is raised.
Private Sub printDocument_PrintPage(sender As Object, ev As PrintPageEventArgs) Handles printDocument.PrintPage
' Draw a picture.
ev.Graphics.DrawImage(System.Drawing.Image.FromFile(saveString),
ev.Graphics.VisibleClipBounds)
' Indicate that this is the last page to print.
ev.HasMorePages = False
End Sub
End Class
我已经看到了这方面的答案,但是当表单基本上是一个弹出窗口自动运行并关闭应用程序时。我怎么处理这个错误?
答案 0 :(得分:1)
显然,您在表单构造函数中调用print()
方法,而不是最初声明的Load
形式的事件。它有什么不同?那么,有两个问题:
saveString
)后设置了New ShippingLabelPrintApp
的值,因此,print()
方法在未初始化saveString
且ev.Graphics.DrawImage(Image.FromFile(saveString),..
的情况下被调用} 将失败。但是,这与ObjectDisposedException
例外无关。print()
),您的New ShippingLabelPrintApp
方法就会被执行。由于print()
方法关闭(即处理)表单,print
方法可以非常快速地完成,调用Form.Close()
,并使表单在{{1}之前处理调用(或ShowDialog()
)(这正是你的情况)。要避免这两个问题,您只需将表单构造函数中的代码移动到表单的Show()
事件中,这样只有在您调用Load
或Form.Show
:
Form.ShowDialog
或者您可以将Private Sub ShippingLabelPrintApp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
print()
End Sub
方法的访问级别设置为print
或Public
,并直接从Friend
调用:
Sub Main
最后一件事是,您真的不需要致电Public Class ShippingLabelPrintApp
Friend Sub print()
'
'
End Sub
End Class
Sub Main()
Dim manualShipForm As New ShippingLabelPrintApp
manualShipForm.saveString = "Your\Path\To\The\Image\File.png"
manualShipForm.Show() 'In this case, don't use ShowDialog so that the next line gets
' executed which takes care of the blocking instead of ShowDialog.
manualShipForm.print()
'Application.Exit()
End Sub
,因为应用程序会在Application.Exit
结束后自动退出。