Properly using PrintPreview dialog

Altin picture Altin · Dec 8, 2012 · Viewed 14.1k times · Source

This is my first time I have to use print preview and I got confused so I need a suggestion:

enter image description here

this is the dialog that I wanna print:

this is how it currently looks in my PrintPreview dialog (messed up): enter image description here

this is my full code at the moment:

Public Class frmPRINT

Private Sub btnPrint_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

    Dim bm As New Bitmap(DataGridView1.Width, DataGridView1.Height)
    DataGridView1.DrawToBitmap(bm, New Rectangle(0, 0, DataGridView1.Width, DataGridView1.Height))
    e.Graphics.DrawImage(bm, 0, 0)

End Sub

Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click

    PrintPreviewDialog1.WindowState = FormWindowState.Normal
    PrintPreviewDialog1.StartPosition = FormStartPosition.CenterScreen
    PrintPreviewDialog1.ClientSize = New Size(600, 600)

    PrintPreviewDialog1.Document = PrintDocument1
    PrintPreviewDialog1.ShowDialog()
End Sub
End Class

how can I set this up properly please?

Answer

Ken White picture Ken White · Dec 9, 2012

Set the page orientation to landscape with

printDocument1.DefaultPageSettings.Landscape = true

For more information, see the MSDN documentation on PrintDocument.DefaultPaperSettings.