Results 1 to 5 of 5

Thread: Printing a Windows form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    158

    Question Printing a Windows form

    I am trying to do something that should be simple: In my vb.net Windows Form application, when the F1 key is pressed the form should be printed. I do not need confirmation dialogs or anything like that.

    I tried the following code (which I found on a website or two):
    Code:
    Private WithEvents PrintDocument1 As PrintDocument = New PrintDocument
    
    Private Sub frmDGVTest_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.F1 Then
            AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintImage
            PrintDocument1.Print()
        End If
    End Sub
    
    Private Sub PrintImage(ByVal sender As Object, ByVal ppea As PrintPageEventArgs)
        Dim bmp As New Bitmap(Me.Width, Me.Height)
        Me.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Width, Me.Height))
    
        ppea.Graphics.DrawImage(bmp, ppea.Graphics.VisibleClipBounds)
        ppea.HasMorePages = False
    End Sub
    Subroutine frmDGVTest_KeyDown works just fine, but the other doesn’t work. In fact, when I try running the code, the program hangs, cannot be killed via the Task Manager, and the computer needs to be rebooted. Please fix my code!

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: Printing a Windows form

    Here is an official MSDN documentation on how to print a form. https://learn.microsoft.com/en-us/do...t-windows-form

    Here is a modified example to fit your use case:
    Code:
    Public Class frmDGVTest
        Private WithEvents _printDocument As PrintDocument
        Private _memoryImage As Bitmap
    
        Private Sub frmDGVTest_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
            If (e.KeyCode = Keys.F1) Then
                Using myGraphics = Me.CreateGraphics()
                    Dim formSize = Me.Size
                    _memoryImage = New Bitmap(formSize.Width, formSize.Height, myGraphics)
                    Using memoryGraphics = Graphics.FromImage(memoryImage)
                        memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, formSize)
                    End Using
                End Using
            
                PrintDocument1.Print()
            End If
        End Sub
    
        Private Sub PrintImage(sender As Object, e As PrintPageEventArgs) Handles _printDocument.PrintPage
            e.Graphics.DrawImage(_memoryImage, 0, 0)
        End Sub
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Printing a Windows form

    WithEvents Is used to specify that a component included via code can have eventhandlers specified in code, so rather than using AddHandler…

    Code:
    Private Sub PrintImage(ByVal sender As Object, ByVal ppea As PrintPageEventArgs) Handles PrintDocument1.PrintPage

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    158

    Re: Printing a Windows form

    Thank you dday9 and .paul. When I used the provided code, I got an error. The problem is this: I put a break point on the line 'PrintDocument1.Print()' and the debugger shows PrintDocument1 as being nothing (once I changed the name of the declared variable to match). The referenced code has the same issue - printDocument1 is never defined.

    Okay - just solved the problem: the declaration should be
    Code:
    Private WithEvents _printDocument As New PrintDocumen

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: Printing a Windows form

    Yes, that was my problem. I free-typed the code and forgot to initialize the class. Thinking about it a bit more, unless you plan on changing the value of the variable, you should probably mark it as readonly too.
    Code:
    Private WithEvents _printDocument As PrintDocument = New PrintDocument()
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

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