Print and Print Preview Help

Dr@g0nic

Expert Member
Joined
Oct 11, 2006
Messages
1,654
Reaction score
4
Location
Somerset West, Cape Town
Hey guys

Hey guys

I would just like like to know how to add printing and print preview in a windows form...

i still have my books from college and i've looked on the net and call me dumb but it dont make much sense (or maybe im lazy)... is there anybody that can help out more
 
Well there are a couple of ways you can do it, if you are using the reports in visual studio 2005 then you can use their built in report viewer control which allows for print preview and printing...
 
ok cool thanks

and any other way that you can think of?

for example i've added the PrintPreviewDialog, PrintPreviewControl(use it at run time), PrintDocument, PageSetupDialog

this is the code i've used (probably lots of things missing)

Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click

PreviewDialog.WindowState = FormWindowState.Maximized
PreviewDialog.PrintPreviewControl.StartPage = 0
PreviewDialog.Document = PrintDocument1

PreviewDialog.ShowDialog()

End Sub


when i click print preview i get the print preview screen but its blank:confused:
 
Have you populated the the print document with data... You need to draw on a print document using the graphics functions.
What do you want to display in the document?
 
yes yes thats the thing...

it says in this book you have to use GDI+ drawing methods of the PrintPageEventArgs.Graphics object to construct contents of the print document page....

but i'm feeling dumb that i dunno where or how

picturebox, labels and textboxes.
its a program im designing for my dads work
 
If its just a form that you want to design I would recommend using the built in report document, there you can just drag and drop components...

Are you using the full version of vb.net 2005
 
Here is a simple example I put together. I used it to manually create reports last year for a project.

My VB is a bit rusty, been using C# ever since I switched to .NET :D

Code:
    Friend WithEvents myReport As System.Drawing.Printing.PrintDocument
    Dim pageNum As Integer
    Dim myFont As System.Drawing.Font


    Private Sub generatePreview()
        pageNum = 1
        myFont = New Font("Arial", 16, CType(System.Drawing.FontStyle.Bold, System.Drawing.FontStyle))
        myReport = New System.Drawing.Printing.PrintDocument
        Dim ppd As PrintPreviewDialog = New PrintPreviewDialog
        ppd.Document = myReport
        ppd.WindowState = FormWindowState.Maximized
        ppd.PrintPreviewControl.Zoom = 0.5
        ppd.ShowDialog()
    End Sub

    Private Sub PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles myReport.PrintPage

        'heading
        e.Graphics.DrawString("Hello World", myFont, Brushes.Blue, 50, 50)

        'checker board
        Dim j As Integer
        Dim k As Integer

        For j = 0 To 7
            For k = 0 To 7
                If (j + k) Mod 2 = 0 Then

                    e.Graphics.FillRectangle(Brushes.Black, 100 + j * 40, 100 + k * 40, 40, 40)
                Else
                    e.Graphics.FillRectangle(Brushes.White, 100 + j * 40, 100 + k * 40, 40, 40)
                End If

            Next k
        Next j

        e.Graphics.DrawRectangle(Pens.Black, 100, 100, j * 40, k * 40)

        'page numbering
        Dim stringSize As SizeF = e.Graphics.MeasureString("Page " & pageNum, myFont)
        e.Graphics.DrawString("Page " & pageNum, myFont, Brushes.Black, e.PageSettings.PrintableArea.Width - stringSize.Width, e.PageSettings.PrintableArea.Height - stringSize.Height)


        If pageNum < 2 Then
            e.HasMorePages = True
        End If
        pageNum += 1
    End Sub

Anyhow, hope this helps.
 
To buy that control is damn expensive, unless wesleyfraser has a copy that fell off the back of a truck if you know what I mean ;).

I figured I could write one myself, here is my first attempt it prints a form to a single page. It doesn't scale larger forms down, if you need this I'm sure I can add it.

Give it a try if you need anymore features let me know.

Control + Example

Oh and for hosting images try imageshack or one of the many other image hosts, Google is your friend.

Edit: If you feel uneasy running this control let me know I'll gladly send you the source code however, its in C#.
 
Top
Sign up to the MyBroadband newsletter
X