Results 1 to 9 of 9

Thread: [RESOLVED] VB.NET Error while printing

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    15

    [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.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    15

    Angry Re: VB.NET Error while printing

    Quote Originally Posted by Niya View Post
    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.

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: VB.NET Error while printing

    Two suggestions

    1. Remove the Dispose method
    2. In the code which does the AddHandler first do a RemoveHandler for each AddHandler.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    15

    Angry Re: VB.NET Error while printing

    Quote Originally Posted by kevininstructor View Post
    Two suggestions

    1. Remove the Dispose method
    2. 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&#225;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&#225;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&#225;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.

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: VB.NET Error while printing

    Show the code you are using to show this form.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    15

    Red face Re: VB.NET Error while printing

    Quote Originally Posted by kevininstructor View Post
    Show the code you are using to show this form.
    This form is called by a button and is code is simply

    PrintSelectedDocument.ShowDialog()

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: VB.NET Error while printing

    Quote Originally Posted by Tedioboy View Post
    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

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    15

    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 &#171;Using&#187; 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&#225;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
  •  



Click Here to Expand Forum to Full Width