Printing second time fails

R

Rick Lederman

I am using a PrintDocument and PrintDialog to print. The first time that I
print it works, but when I try to print a second time without exiting the
entire program I get an "InvalidPrinterException was unhandled" error. The
error happens in the third line below. The details of the error are below
my signature. Help?

PrintDialog1.PrinterSettings.PrinterName = sLabelPrinterDeviceName

PrintDialog1.Document = PrintDocument1

PrintDialog1.Document.Print() ' the error happens in this line



Any ideas?

--
Rick Lederman
Soaring Software Solutions, Inc.
www.soaringsoftware.com
(e-mail address removed)

System.Drawing.Printing.InvalidPrinterException was unhandled
Message="Settings to access printer 'Samsung ML-2250 Series (Copy 1)' are
not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal()
at System.Drawing.Printing.PrinterSettings.GetHdevmode(PageSettings
pageSettings)
at System.Drawing.Printing.PrintController.OnStartPrint(PrintDocument
document, PrintEventArgs e)
at
System.Windows.Forms.PrintControllerWithStatusDialog.OnStartPrint(PrintDocument
document, PrintEventArgs e)
at System.Drawing.Printing.PrintController.Print(PrintDocument
document)
at System.Drawing.Printing.PrintDocument.Print()
at TCMaxLabelMaker.frmMain.cmdPrintSequence_Click(Object sender,
EventArgs e) in C:\VS2005\Copy (15) of
TCMaxLabelMaker\TCMaxLabelMaker\frmMain.vb:line 4932
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine)
at TCMaxLabelMaker.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext, String[] activationCustomData)
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
G

Guest

Rick,

I suspect the problem is in the printdocument's print event.
Could you please post the code in that event.

Ken
----------------

Rick Lederman said:
I am using a PrintDocument and PrintDialog to print. The first time that I
print it works, but when I try to print a second time without exiting the
entire program I get an "InvalidPrinterException was unhandled" error. The
error happens in the third line below. The details of the error are below
my signature. Help?

PrintDialog1.PrinterSettings.PrinterName = sLabelPrinterDeviceName

PrintDialog1.Document = PrintDocument1

PrintDialog1.Document.Print() ' the error happens in this line



Any ideas?

--
Rick Lederman
Soaring Software Solutions, Inc.
www.soaringsoftware.com
(e-mail address removed)

System.Drawing.Printing.InvalidPrinterException was unhandled
Message="Settings to access printer 'Samsung ML-2250 Series (Copy 1)' are
not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal()
at System.Drawing.Printing.PrinterSettings.GetHdevmode(PageSettings
pageSettings)
at System.Drawing.Printing.PrintController.OnStartPrint(PrintDocument
document, PrintEventArgs e)
at
System.Windows.Forms.PrintControllerWithStatusDialog.OnStartPrint(PrintDocument
document, PrintEventArgs e)
at System.Drawing.Printing.PrintController.Print(PrintDocument
document)
at System.Drawing.Printing.PrintDocument.Print()
at TCMaxLabelMaker.frmMain.cmdPrintSequence_Click(Object sender,
EventArgs e) in C:\VS2005\Copy (15) of
TCMaxLabelMaker\TCMaxLabelMaker\frmMain.vb:line 4932
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine)
at TCMaxLabelMaker.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext, String[] activationCustomData)
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
R

Rick Lederman

Ken,

Thanks for the help as I am at a loss. Here is the code:
The code looses its format here ... should I post the code some other way?
--
Rick Lederman
Soaring Software Solutions, Inc.

Dim font As New Font("Times New Roman", CSng(txtFontSize.Text),
FontStyle.Regular, GraphicsUnit.Point)

Dim TextWidth As Single

Dim TextHeight As Single

Dim AdjustedBarcodeScale As Single

lCol = -1

lRow = 1

iX = dLeftMargin

iY = dTopMargin

iStart = iCurrent

For iI = iStart To iEnd

tdbLabels.Row = iI

iCopies = CInt(tdbLabels.Columns(2).Value)

For iJ = 1 To iCopies

AdjustedBarcodeScale = BarcodeScale

BarcodeProfessional1.Code = tdbLabels.Columns(0).Text.ToString

Dim BarcodeImage As Image = BarcodeProfessional1.GetBarcodeImage()

Dim realBarcodeWidth As Integer = BarcodeImage.Width * BarcodeScale ' -
BarcodeProfessional1.QuietZoneWidth * 2

Dim realBarcodeHeight As Integer = BarcodeImage.Height * BarcodeScale

If realBarcodeWidth > (dLabelWidth - dLabelWidth * 0.1) Then

Dim MakeSmaller As Single

MakeSmaller = (dLabelWidth - dLabelWidth * 0.1) / BarcodeImage.Width

AdjustedBarcodeScale = BarcodeScale * MakeSmaller

realBarcodeWidth = BarcodeImage.Width * AdjustedBarcodeScale

realBarcodeHeight = BarcodeImage.Height * AdjustedBarcodeScale

End If

lCol = lCol + 1

iCurrent = iI

If lCol > lColumns - 1 Then

iX = dLeftMargin + dLabelWidth / 2 - realBarcodeWidth / 2 ' this does not
work

iY = iY + dDistanceBetweenRows + dBarcodeHeight

lRow = lRow + 1

lCol = 0

Else

iX = dLeftMargin + dLabelWidth / 2 - realBarcodeWidth / 2 + lCol *
(dDistanceBetweenColumns + dLabelWidth)

End If

If lRow > lRows Then

e.HasMorePages = True

Exit For

Else

e.HasMorePages = False

End If

Dim StringSizeTop As SizeF =
e.Graphics.MeasureString(txtMessage.Text.ToString, font)

TextWidth = StringSizeTop.Width

TextHeight = StringSizeTop.Height

e.Graphics.DrawString(txtMessage.Text.ToString, font, Brushes.Black, New
PointF(dLeftMargin + dLabelWidth / 2 - TextWidth / 2 + lCol *
(dDistanceBetweenColumns + dLabelWidth), iY - TextHeight))

sTemp.Remove(0, sTemp.Length)

sTemp.Append(tdbLabels.Columns(0).Text.ToString & " " &
tdbLabels.Columns(1).Text.ToString)

Dim StringSizeBottom As SizeF = e.Graphics.MeasureString(sTemp.ToString,
font)

TextWidth = StringSizeBottom.Width

TextHeight = StringSizeBottom.Height

e.Graphics.DrawString(sTemp.ToString, font, Brushes.Black, New
PointF(dLeftMargin + dLabelWidth / 2 - TextWidth / 2 + lCol *
(dDistanceBetweenColumns + dLabelWidth), iY + realBarcodeHeight))

BarcodeProfessional1.DrawOnCanvas(e.Graphics, New PointF(iX, iY),
AdjustedBarcodeScale) ' this is in inches!

Next iJ

If e.HasMorePages = True Then

Exit For

End If

Next iI
 
R

Rick Lederman

Ken,

I just took ALL of the code out of the PrintDocument2_PrintPage event and it
feeds a page the first time that it is run and has the error the second
time! So, it must not be the ocde in the PrintPage event?

Private Sub PrintDocument2_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage



Here is the code that I use in the Button_Click event (the end of the code
anyway)

If chkLandscape.CheckState = System.Windows.Forms.CheckState.Unchecked Then

PrintDocument2.DefaultPageSettings.Landscape = False

Else

PrintDocument2.DefaultPageSettings.Landscape = True

End If

PrintDialog2.PrinterSettings.PrinterName = sLabelPrinterDeviceName ' I set
this to the printer name very early in the program

PrintDialog2.Document = PrintDocument2

PrintDialog2.Document.Print() ' this line works the first time, and errors
the second time. Then I must restart the program.
 
G

Guest

Rick,

I see you are using a variable iCurrent to keep track of the
record number. I do not see where you reset iCurrent to the begining of the
list when you print the second time.

Ken
 
R

Rick Lederman

Ken,

I reset it at the beginning of the CmdPrint_Click event. THat is the button
that I click to do the print job. And, as I said, I deleted all of the code
in the PrintDocument2_PrintPage event and it still fails. Here is all of
the code in the CmdPrint_Click event.

Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdPrint.Click

lCol = 0

lRow = 0

MyFlags.PrintingEmployees = False

MyFlags.PrintingTools = True

iStart = 0

iCurrent = 0

iEnd = CInt(tdbLabels.RowCount - 1)

If iStart > iEnd Then

MsgBox(MyLabels.IncorrectSequence, MsgBoxStyle.Information, "Sequence
incorrect")

Exit Sub

End If

iCopies = 1

BarcodeProfessional1.AutoSize = True

Select Case cmbBarcodeStyle.SelectedIndex

Case Is = 0

BarcodeProfessional1.Symbology = Symbology.Code39

BarcodeScale = 1

Case Is = 1

BarcodeProfessional1.Symbology = Symbology.Code93

BarcodeScale = 1

Case Is = 2

BarcodeProfessional1.Symbology = Symbology.DataMatrix

BarcodeProfessional1.DataMatrixFormat = DataMatrixFormat.Auto

BarcodeScale = CSng(txtCellSize.Text) / 14 * 0.39

Case Is = 3

BarcodeProfessional1.Symbology = Symbology.DataMatrix

BarcodeProfessional1.DataMatrixFormat = DataMatrixFormat.C12X36

BarcodeScale = CSng(txtCellSize.Text) / 14 * 0.39

Case Is = 4

BarcodeProfessional1.Symbology = Symbology.DataMatrix

BarcodeProfessional1.DataMatrixFormat = DataMatrixFormat.C16X36

BarcodeScale = CSng(txtCellSize.Text) / 14 * 0.39

Case Is = 5

BarcodeProfessional1.Symbology = Symbology.Code128

BarcodeScale = 1

Case Is = 6

BarcodeProfessional1.Symbology = Symbology.Interleaved2of5

BarcodeScale = 1

Case Is = 7

BarcodeProfessional1.Symbology = Symbology.Pdf417

BarcodeScale = 1

End Select

BarcodeProfessional1.DataMatrixModuleSize = 4 ' CInt(txtCellSize.Text) - 10

dBarcodeHeight = CSng(cZero((txtBarcodeHeight.Text) * 100))

dBarcodeWidth = CSng(cZero(txtBarcodeWidth.Text) * 100)

BarcodeProfessional1.BarHeight = dBarcodeHeight

BarcodeProfessional1.DisplayCode = False

dLabelHeight = CSng(txtBarcodeHeight.Text) * 100

dLabelWidth = CSng(txtLabelWidth.Text) * 100

lColumns = CInt(txtNumberOfColumns.Text)

lRows = CInt(txtNumberOfRows.Text)

dLeftMargin = CSng(txtLeftMargin.Text) * 100

dTopMargin = CSng(txtTopMargin.Text) * 100

dDistanceBetweenRows = CSng(txtRowDistance.Text) * 100

dDistanceBetweenColumns = CSng(txtColumnDistance.Text) * 100

If chkLandscape.CheckState = System.Windows.Forms.CheckState.Unchecked Then

PrintDocument2.DefaultPageSettings.Landscape = False

Else

PrintDocument2.DefaultPageSettings.Landscape = True

End If

PrintDialog2.PrinterSettings.PrinterName = sLabelPrinterDeviceName

PrintDialog2.Document = PrintDocument2

PrintDialog2.Document.Print()



End Sub


--
Rick Lederman
Soaring Software Solutions, Inc.
www.soaringsoftware.com
(e-mail address removed)
 
R

Rick Lederman

Again, does not work. That is strange, the first thime that I print it
everything works. Click the print button again and I get the same
"InvalidPrinterException was unhandled" error. Underneigh that it also says
(same as before) " Settings to access printer 'Samsung ML-2250 'are not
valid." ...

--
Rick Lederman
Soaring Software Solutions, Inc.
www.soaringsoftware.com
(e-mail address removed)
 
R

Rick Lederman

To test things I put the following into the program

If PrintDialog2.PrinterSettings.IsValid = True Then

PrintDialog2.Document.Print() ' this was already there

End If

And, imagine that the IsValid returns False on the second print job. So the
question remains what makes the PrinterSettings become false after using it
once?

And, during a program run, I set the printer to a different printer, and the
first time that it ran it printed OK, the second time that I tried to print,
IT RETURNED TRUE and ran OK! And it continues to work for the new printer.

So, I change the printer selector back to the original printer. And it
WORKS ... these kinds of problems I really don't need, but for now it is all
WORKING ...

Ken, thanks for your help. I have no idea what was going on within this PC
but simply printing to a different printer then switching back to this
printer seems to have fixed it. The magic of Windows!

--
Rick Lederman
Soaring Software Solutions, Inc.
www.soaringsoftware.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top