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:
'This code is in the mdi parent and is used to print a preview of the 'currently selected mdi form Private Sub mnuPrintPreview_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuPrintPreview.Click 'is this the best way to reset the printpreviewdialog? PrintPreviewDialog1.Document = Nothing Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32 With PrintDocument1.DefaultPageSettings intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right marginLeft = .Margins.Left marginTop = .Margins.Top End With ' If the user selected Landscape mode, swap the area height and width. If PrintDocument1.DefaultPageSettings.Landscape Then Dim intTemp As Int32 intTemp = intPrintAreaHeight intPrintAreaHeight = intPrintAreaWidth intPrintAreaWidth = intTemp End If Dim frmCurrent As Form = Me.ActiveMdiChild Select Case frmCurrent.Name Case "frmCFRReport" 'pass a print area rectangle to the form frmCFRReport.PrintAreaRectangle = New Rectangle(marginLeft, _ marginTop, intPrintAreaWidth, intPrintAreaHeight) frmCFRReport.PageSettings = PrintDocument1.DefaultPageSettings 'set the printpreviewdialog1 to the childform printpreviewdocument PrintPreviewDialog1.Document = frmCFRReport.PrintCFRDocument Case else 'Other cases here End Select If PrintPreviewDialog1.Document IsNot Nothing Then PrintPreviewDialog1.ShowDialog() End If End Sub
VB Code:
'this is the mdi child form printpreviewdocument print event Public Sub PrintCFRDocument_PrintPage(ByVal sender As System.Object, _ ByVal e As System.Drawing.Printing.PrintPageEventArgs) _ Handles PrintCFRDocument.PrintPage e.Graphics.Clear(Color.White) Threading.Thread.Sleep(50) e.Graphics.DrawString(lblMainHeading.Text, MainFont, TextColour, _ recPrintArea.Left + lblMainHeading.Left, recPrintArea.Top + _ lblMainHeading.Top) Threading.Thread.Sleep(50) e.Graphics.FillRectangle(SubHeadingGroupColour, recPrintArea.Left _ + grpExpenditure.Left + intExpXOffset, recPrintArea.Top + _ grpExpenditure.Top + intExpYOffset, grpExpenditure.Width, _ grpExpenditure.Height) Threading.Thread.Sleep(50) End Sub
Any help would be GREATLY appreciated. I seem to be running around in circles here.




Reply With Quote