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!