Я вызываю форму, которая, как я ожидаю, автоматически запускает некоторый код, а затем снова закрывается. Когда я вызываю 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
Я видел ответы на этот вопрос, но не тогда, когда форма представляет собой всплывающее окно, автоматически запускаемое и закрывающее приложение. Как я справляюсь с этим неправильно?