|
-
Jan 15th, 2012, 09:47 AM
#1
Thread Starter
New Member
[RESOLVED] VB.NET Error while printing
Hi there!
I've got problems while printing a document.
I've got a program that have a small form with options for printing two diferent pages. Here's the code:

There is a Button for printing named Imprimir and two checkboxes for selecting what king of display I want to send to printer.
At first time, there are no problems, the printer prints what I want, the form closes and it seems all is allright. The problem is when I try to print again, even the same layout, I call the print form again and I get the error in the picture...
What am I doing wrong?
Why the same routine executes without problem at first instance and gives this error at second instante?
Thanks in advance.
Best regards.
Tedioboy, from Portugal.
Last edited by Tedioboy; Jan 15th, 2012 at 02:32 PM.
-
Jan 15th, 2012, 10:33 AM
#2
Re: VB.NET Error while printing
When the application break on that error, check the parameter values for DrawString to make sure that they are what they are supposed to be. Ie Check the values of the variables "Tf", "PgLeft", "PgWidth" and Strf.
-
Jan 15th, 2012, 11:08 AM
#3
Thread Starter
New Member
Re: VB.NET Error while printing
 Originally Posted by Niya
When the application break on that error, check the parameter values for DrawString to make sure that they are what they are supposed to be. Ie Check the values of the variables "Tf", "PgLeft", "PgWidth" and Strf.
These setting are some lines above in the code... the variables are all declared as single.
At first instance, there are no problems with the document print. Program prints to the printer without errors butm if I try to print another time I get this error.
Why the error's nor created at first print?
First print without problems, code is good. Seems to be some information about printer or it configuration I don't erase between documents print, I think, but how to do that?
Thanks anyway.
-
Jan 15th, 2012, 12:13 PM
#4
Re: VB.NET Error while printing
Two suggestions
- Remove the Dispose method
- In the code which does the AddHandler first do a RemoveHandler for each AddHandler.
-
Jan 15th, 2012, 01:06 PM
#5
Thread Starter
New Member
Re: VB.NET Error while printing
 Originally Posted by kevininstructor
Two suggestions
- Remove the Dispose method
- In the code which does the AddHandler first do a RemoveHandler for each AddHandler.
The same error occurs.
Here is all code (resumed):
Code:
Private Sub FormActivated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
'Clears a listbox containing all the printers installed
LstImprs.Items.Clear()
'Put all printer names in the listbox
For Each Item As String In PrinterSettings.InstalledPrinters
LstImprs.Items.Add(Item)
Next
'Selects the default printer in the listbox
LstImprs.SelectedIndex = LstImprs.Items.IndexOf((New System.Drawing.Printing.PrinterSettings).PrinterName)
'Button «Imprimir» only becames enabled if there's a printer installed
Imprimir.Enabled = (LstImprs.SelectedIndex <> -1)
End Sub
Private Sub Imprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Imprimir.Click
Vista = New PrintDocument
RemoveHandler Vista.PrintPage, AddressOf Me.DocPrintVistaMensal
RemoveHandler Vista.PrintPage, AddressOf Me.DocPrintVistaAnual
'There are two checkboxes in Main form where I select witch Page I want to print.
If Base.VistaMensal.Checked Then AddHandler Vista.PrintPage, AddressOf Me.DocPrintVistaMensal
If Base.VistaAnual.Checked Then AddHandler Vista.PrintPage, AddressOf Me.DocPrintVistaAnual
Vista.PrinterSettings.PrinterName = LstImprs.SelectedItem
If Vista.PrinterSettings.IsValid Then Vista.Print()
Me.Close()
End Sub
Private Sub DocPrintVistaMensal(ByVal sender As Object, ByVal e As PrintPageEventArgs)
e.Graphics.PageUnit = GraphicsUnit.Millimeter
Dim PgLeft As Single = e.MarginBounds.Left * 0.254,
PgWidth As Single = e.MarginBounds.Width * 0.254,
PgBottom As Single = e.MarginBounds.Bottom * 0.254
Using B As Brush = Brushes.Black, StrF As New StringFormat(StringFormatFlags.NoClip)
StrF.Alignment = StringAlignment.Center
Dim Tf As Font = New Font("Arial", 24, FontStyle.Bold)
StrF.LineAlignment = StringAlignment.Center
e.Graphics.DrawString("Cálculo de Vencimentos", Tf, B, New RectangleF(PgLeft, 25, PgWidth, 10), StrF)
'(Rest of the print code removed)
End Using
e.HasMorePages = False
End Sub
Private Sub DocPrintVistaAnual(ByVal sender As Object, ByVal e As PrintPageEventArgs)
e.Graphics.PageUnit = GraphicsUnit.Millimeter
Dim PgLeft As Single = e.MarginBounds.Left * 0.254,
PgWidth As Single = e.MarginBounds.Width * 0.254,
PgBottom As Single = e.MarginBounds.Bottom * 0.254
Using B As Brush = Brushes.Black, StrF As New StringFormat(StringFormatFlags.NoClip)
StrF.Alignment = StringAlignment.Center
Dim Tf As Font = New Font("Arial", 24, FontStyle.Bold)
StrF.LineAlignment = StringAlignment.Center
e.Graphics.DrawString("Cálculo de Vencimentos", Tf, B, New RectangleF(PgLeft, 25, PgWidth, 10), StrF)
'(Rest of the print code removed)
End Using
e.HasMorePages = False
End Sub
I've tryed this way too:
Code:
Private Sub Imprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Imprimir.Click
Vista = New PrintDocument
Vista.PrinterSettings.PrinterName = LstImprs.SelectedItem
If Base.VistaMensal.Checked Then AddHandler Vista.PrintPage, AddressOf Me.DocPrintVistaMensal
If Base.VistaAnual.Checked Then AddHandler Vista.PrintPage, AddressOf Me.DocPrintVistaAnual
If Vista.PrinterSettings.IsValid Then Vista.Print()
If Base.VistaMensal.Checked Then RemoveHandler Vista.PrintPage, AddressOf Me.DocPrintVistaMensal
If Base.VistaAnual.Checked Then RemoveHandler Vista.PrintPage, AddressOf Me.DocPrintVistaAnual
Me.Close()
End Sub
with same error result...
Here's the exception detail:
Code:
System.ArgumentException was unhandled
Message=Parameter is not valid.
Source=System.Drawing
StackTrace:
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
at Cálculo_de_Vencimentos.DefImprimir.DocPrintVistaAnual(Object sender, PrintPageEventArgs e) in (........).vb:line 180
at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e)
at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e)
at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document)
at System.Drawing.Printing.PrintController.Print(PrintDocument document)
at System.Drawing.Printing.PrintDocument.Print()
At first command, the printer works perfectly. If I try another copy, I get the error...
What is wrong in the code?
I can't find the bug...
Last edited by Tedioboy; Jan 15th, 2012 at 02:03 PM.
-
Jan 15th, 2012, 02:01 PM
#6
Re: VB.NET Error while printing
Show the code you are using to show this form.
-
Jan 15th, 2012, 02:12 PM
#7
Thread Starter
New Member
Re: VB.NET Error while printing
 Originally Posted by kevininstructor
Show the code you are using to show this form.
This form is called by a button and is code is simply
PrintSelectedDocument.ShowDialog()
-
Jan 15th, 2012, 02:18 PM
#8
Re: VB.NET Error while printing
 Originally Posted by Tedioboy
This form is called by a button and is code is simply
PrintSelectedDocument.ShowDialog()
Try this
Code:
Dim f As New PrintSelectedDocument
Try
f.ShowDialog()
Finally
f.Dispose()
End Try
-
Jan 15th, 2012, 02:31 PM
#9
Thread Starter
New Member
Re: VB.NET Error while printing
Thanks a lot, Kevin, for your time, but I found the solution... and it is the stupidest solution I ever see...
I retired the «Using» command and I've declared the variables in Using command.
Yes... seems completly stupid but with this code
Code:
Private Sub DocPrintVistaMensal(ByVal sender As Object, ByVal e As PrintPageEventArgs)
e.Graphics.PageUnit = GraphicsUnit.Millimeter
Dim PgLeft As Single = e.MarginBounds.Left * 0.254,
PgWidth As Single = e.MarginBounds.Width * 0.254,
PgBottom As Single = e.MarginBounds.Bottom * 0.254
Dim B As Brush = Brushes.Black, StrF As New StringFormat(StringFormatFlags.NoClip)
StrF.Alignment = StringAlignment.Center
Dim Tf As Font = New Font("Arial", 24, FontStyle.Bold)
StrF.LineAlignment = StringAlignment.Center
e.Graphics.DrawString("Cálculo de Vencimentos", Tf, B, New RectangleF(PgLeft, 25, PgWidth, 10), StrF)
'(Rest of the print code removed)
e.HasMorePages = False
End Sub
there is no more problems, I can print without problems any times I wish!!!
Thanks for your time anyway.
Best regards.
Tedioboy.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|