Results 1 to 3 of 3

Thread: Problem with PrintPreviewDialog

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    78

    Problem with PrintPreviewDialog

    I have a form with a datagridview I use to show the user the results. I was having a problem when I used PrintPreviewDialog1.Show() that it would only allow the Print preview to be shown once. If the user clicked on the Print preview button twice the program stopped with this error "Cannot access a disposed object. Object name: 'PrintPreviewDialog'." So I changed to code to PrintPreviewDialog1.ShowDialog() now the print preview comes up blank the first time but shows the report after they close the window and click the button again. What am I missing?
    Here is the report
    Name:  RecincileReport.jpg
Views: 671
Size:  19.0 KB
    First Click
    Name:  RecincileReport1.jpg
Views: 558
Size:  11.6 KB
    After close and reopen
    Name:  RecincileReport2.jpg
Views: 583
Size:  19.4 KB

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,548

    Re: Problem with PrintPreviewDialog

    You need to post the relevant code.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    78

    Re: Problem with PrintPreviewDialog

    This is embarrassing. I put this together from several code sources and some how got the AddHandler after the PrintPreviewDialog1.ShowDialog(). A friend looked at the code and spotted the problem right off. Thank you for your time.

    Code:
        Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
    
            PrintDocument1.DefaultPageSettings.Landscape = True
            Dim margins As New Margins(50, 100, 100, 100)
            PrintDocument1.DefaultPageSettings.Margins = margins
    
            PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
            PrintPreviewDialog1.Document = PrintDocument1
    
            AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
            PrintPreviewDialog1.ShowDialog()
    
        End Sub
    
        Protected Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
            Dim ColumnLefts As New ArrayList  ' Left Coordinate of Columns
            Dim ColumnWidths As New ArrayList ' Width of Columns
            Dim ColumnTypes As New ArrayList  ' DataType of Columns
            Dim HeaderHeight As Int16 = 0
    
            Dim ColumnCount As Integer = DataGridView1.ColumnCount
            Dim RowCount As Integer = DataGridView1.RowCount
            Dim Title_Text As String = "ACS Import Balancing Report"
            Dim CellTopPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Top
    
            ' Draw Page Title 
            ' ACS Import Balanceing Report                                  Friday, January 
            e.Graphics.DrawString(Title_Text, New Font(DataGridView1.Font, FontStyle.Bold), _
            Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - _
            e.Graphics.MeasureString(Title_Text, New Font(DataGridView1.Font, _
            FontStyle.Bold), e.MarginBounds.Width).Height - 13)
    
            Dim tmpTop As Int16 = e.MarginBounds.Top
            Dim s As String = Now.ToLongDateString + " " + Now.ToShortTimeString
    
            e.Graphics.DrawString(s, New Font(DataGridView1.Font, FontStyle.Bold), _
               Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - _
               e.Graphics.MeasureString(s, New Font(DataGridView1.Font, FontStyle.Bold), _
               e.MarginBounds.Width).Width), e.MarginBounds.Top - _
               e.Graphics.MeasureString(Title_Text, _
               New Font(New Font(DataGridView1.Font, FontStyle.Bold), FontStyle.Bold), _
               e.MarginBounds.Width).Height - 13)
    
            ' ********************
            ' Draw Columns headers
            ' ********************
            Dim tmpWidth As Int16, i As Int16
            Dim tmpLeft As Int16 = e.MarginBounds.Left
    
            For Each GridCol As DataGridViewColumn In DataGridView1.Columns
                ' Detemining whether the columns are fitted to page or not.
                tmpWidth = GridCol.Width + 5
                HeaderHeight = e.Graphics.MeasureString(GridCol.HeaderText, _
                               GridCol.InheritedStyle.Font, tmpWidth).Height + 11
    
                ColumnLefts.Add(tmpLeft)
                ColumnWidths.Add(tmpWidth)
                ColumnTypes.Add(GridCol.GetType)
                tmpLeft += tmpWidth
            Next
    
            Dim StrFormat As StringFormat
            StrFormat = New StringFormat
            StrFormat.Alignment = StringAlignment.Near
            StrFormat.LineAlignment = StringAlignment.Center
            StrFormat.Trimming = StringTrimming.EllipsisCharacter
            tmpTop = e.MarginBounds.Top
            i = 0
            For Each GridCol As DataGridViewColumn In DataGridView1.Columns
                e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), _
                                         New Rectangle(ColumnLefts(i), tmpTop, ColumnWidths(i), HeaderHeight))
                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, _
                                      New SolidBrush(GridCol.InheritedStyle.ForeColor), _
                                      New RectangleF(ColumnLefts(i), tmpTop, ColumnWidths(i), HeaderHeight), StrFormat)
                i += 1
    
            Next
            CellTopPos += HeaderHeight
    
            ' Draw Data rows 
            For Row = 0 To RowCount - 1
                Dim CellLeftPos As Integer = 50
                For Cell = 0 To ColumnCount - 1
                    Dim CellValue As String = DataGridView1.Rows(Row).Cells(Cell).Value.ToString()
                    Dim CellWidth = DataGridView1.Rows(Row).Cells(Cell).Size.Width + 5
                    Dim CellHeight = DataGridView1.Rows(Row).Cells(Cell).Size.Height
    
                    Dim Brush As New SolidBrush(Color.Black)
                    e.Graphics.DrawString(CellValue, New Font("arial", 9), Brush, CellLeftPos, CellTopPos)
                    e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)
    
                    CellLeftPos += CellWidth
                Next
                CellTopPos += DataGridView1.Rows(Row).Cells(0).Size.Height
            Next
    
            'MsgBox("Done")
    
        End Sub

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