Krishanu Dey
Krishanu Dey

Reputation: 6406

Closing form after printing a web browser control contained in it

I'm taking some random print through the html and web browser control in vb.net winforms Here is my code

Public Class Form1

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim myWebBrowser As New WebBrowser
    AddHandler myWebBrowser.DocumentCompleted, AddressOf DocumentCompleted
    myWebBrowser.Navigate("http://www.bing.com")

  End Sub

  Private Sub DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)

    With DirectCast(sender, WebBrowser)
      If .ReadyState = WebBrowserReadyState.Complete Then
        .Print()
      End If
    End With
  End Sub

End Class

I want the form to be closed after the print. Now if I write Me.Close() after .Print() nothing is being printed. what should I do to achieve this?

Any help is appreciated.

::Update::

after @Noseratio's suggestion I tried to handle the event onafterprint in my html and tried to invoke Me.Close() using ObjectFoprScripting set to my form. But that is firing the close method without any print.

Here is my code

script tag in my html page

<script>
    function window.onafterprint() {
        window.external.Test('called from script code');
    }
</script>

VB.net Code of my form

Imports System.IO
Imports Microsoft.Win32
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.AllowWebBrowserDrop = False
        WebBrowser1.IsWebBrowserContextMenuEnabled = False
        WebBrowser1.WebBrowserShortcutsEnabled = False
        webBrowser1.ObjectForScripting = Me
        WebBrowser1.DocumentText = File.ReadAllText("localprint.htm")
    End Sub

    Public Sub Test(ByVal message As String)
        MessageBox.Show(message, "client code")
        Me.BeginInvoke(DirectCast(Sub() Me.Close(), MethodInvoker))
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        WebBrowser1.Print()
    End Sub
End Class

Upvotes: 1

Views: 1662

Answers (1)

Krishanu Dey
Krishanu Dey

Reputation: 6406

Found my solution

No need to handle onafterprint in javascript.
Here is what I did,

Step 1
Added a reference to SHDocVw.dll in my project. This can be found in c:\windows\system32 folder.

Step2

My new updated code

Imports System.IO
Imports Microsoft.Win32
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.AllowWebBrowserDrop = False
        WebBrowser1.IsWebBrowserContextMenuEnabled = False
        WebBrowser1.WebBrowserShortcutsEnabled = False
        WebBrowser1.DocumentText = File.ReadAllText("localprint.htm")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted_1(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim wb As WebBrowser = TryCast(sender, WebBrowser)

        Dim ie As SHDocVw.InternetExplorer = DirectCast(wb.ActiveXInstance, SHDocVw.InternetExplorer)
        AddHandler ie.PrintTemplateInstantiation, AddressOf IE_OnPrintTemplateInstantiation
        AddHandler ie.PrintTemplateTeardown, AddressOf IE_OnPrintTemplateTeardown

        'Just to get reference of  the webBrowser1 control in ie events, uncomment the below line
        'ie.PutProperty("WebBrowserControl", DirectCast(wb, Object))

        wb.Print()
    End Sub

    Private Sub IE_OnPrintTemplateInstantiation(pDisp As Object)
        ' The PrintTemplateInstantiation event is fired when the print job is starting.
    End Sub

    Private Sub IE_OnPrintTemplateTeardown(pDisp As Object)
        ' The PrintTemplateTeardown event is fired when the print job is done.

        'Just to get reference of  the webBrowser1 control, uncomment the below line
        'Dim iwb2 As SHDocVw.IWebBrowser2 = TryCast(pDisp, SHDocVw.IWebBrowser2)
        'Dim wb As WebBrowser = DirectCast(iwb2.GetProperty("WebBrowserControl"), WebBrowser)

        Me.Close()

    End Sub
End Class

Upvotes: 2

Related Questions