Results 1 to 2 of 2

Thread: [2005] Printpreview errors when calling from mdi parent

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Location
    Manchester, UK
    Posts
    12

    [2005] Printpreview errors when calling from mdi parent

    Here is the elusive error! The error only kicks in sometimes and when it does, the printpreview does not work again without restarting the application.

    PrintPreviewDialog1.ShowDialog()
    ArgumentException was unhandled
    Parameter is not valid.

    I’m not sure what the error is talking about because it works 50% of the time. I thought at first it might have been an issue with the amount of time it took for the graphics drawing which is why I added plenty of pauses inbetween each statement. I can actually comment out all of the code within the print event and it will still not reach it and crash.

    Also if my coding looks horrendous (which I’m sure it might seeing as though printing is all self taught) then any hints would be great too.

    Below are the printpreview menu click event from the mdi parent and the code for the printpreviewdialog print event for the mdi child.

    VB Code:
    1. 'This code is in the mdi parent and is used to print a preview of the
    2.     'currently selected mdi form
    3.     Private Sub mnuPrintPreview_Click(ByVal sender As System.Object, _
    4.         ByVal e As System.EventArgs) Handles mnuPrintPreview.Click
    5.  
    6.         'is this the best way to reset the printpreviewdialog?
    7.         PrintPreviewDialog1.Document = Nothing
    8.  
    9.         Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
    10.         With PrintDocument1.DefaultPageSettings
    11.             intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
    12.             intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
    13.             marginLeft = .Margins.Left
    14.             marginTop = .Margins.Top
    15.         End With
    16.         ' If the user selected Landscape mode, swap the area height and width.
    17.         If PrintDocument1.DefaultPageSettings.Landscape Then
    18.             Dim intTemp As Int32
    19.             intTemp = intPrintAreaHeight
    20.             intPrintAreaHeight = intPrintAreaWidth
    21.             intPrintAreaWidth = intTemp
    22.         End If
    23.  
    24.         Dim frmCurrent As Form = Me.ActiveMdiChild
    25.         Select Case frmCurrent.Name
    26.             Case "frmCFRReport"
    27.                'pass a print area rectangle to the form
    28.                 frmCFRReport.PrintAreaRectangle = New Rectangle(marginLeft, _
    29.                 marginTop, intPrintAreaWidth, intPrintAreaHeight)
    30.                 frmCFRReport.PageSettings = PrintDocument1.DefaultPageSettings
    31.                'set the printpreviewdialog1 to the childform printpreviewdocument
    32.                 PrintPreviewDialog1.Document = frmCFRReport.PrintCFRDocument
    33.             Case else
    34.                 'Other cases here
    35.         End Select
    36.  
    37.         If PrintPreviewDialog1.Document IsNot Nothing Then
    38.             PrintPreviewDialog1.ShowDialog()
    39.         End If
    40.  
    41.     End Sub

    VB Code:
    1. 'this is the mdi child form printpreviewdocument print event
    2.     Public Sub PrintCFRDocument_PrintPage(ByVal sender As System.Object, _
    3.         ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
    4.         Handles PrintCFRDocument.PrintPage
    5.  
    6.         e.Graphics.Clear(Color.White)
    7.         Threading.Thread.Sleep(50)
    8.  
    9.         e.Graphics.DrawString(lblMainHeading.Text, MainFont, TextColour, _
    10.         recPrintArea.Left + lblMainHeading.Left, recPrintArea.Top + _
    11.         lblMainHeading.Top)
    12.         Threading.Thread.Sleep(50)
    13.  
    14.         e.Graphics.FillRectangle(SubHeadingGroupColour, recPrintArea.Left _
    15.         + grpExpenditure.Left + intExpXOffset, recPrintArea.Top + _
    16.         grpExpenditure.Top + intExpYOffset, grpExpenditure.Width, _
    17.         grpExpenditure.Height)
    18.         Threading.Thread.Sleep(50)
    19.     End Sub

    Any help would be GREATLY appreciated. I seem to be running around in circles here.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2005] Printpreview errors when calling from mdi parent

    Add a try/catch statement to your printpage event.

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