title: Closing form after printing a web browser control contained in it
body: <p>I'm taking some random print through the html and web browser control in vb.net winforms
  Here is my code</p>
  
  <pre><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
  </code></pre>
  
  <p>I want the form to be closed after the print. Now if I write <code>Me.Close()</code> after <code>.Print()</code>  nothing is being printed. what should I do to achieve this?</p>
  
  <p>Any help is appreciated.</p>
  
  <p><strong>::Update::</strong></p>
  
  <p>after @Noseratio's suggestion I tried to handle the event <code>onafterprint</code> in my html and tried to invoke <code>Me.Close()</code> using <code>ObjectFoprScripting</code> set to my form. But that is firing the close method without any print.</p>
  
  <p><strong>Here is my code</strong></p>
  
  <blockquote>
    <p>script tag in my html page</p>
  </blockquote>
  
  <pre><code>&lt;script&gt;
      function window.onafterprint() {
          window.external.Test('called from script code');
      }
  &lt;/script&gt;
  </code></pre>
  
  <blockquote>
    <p>VB.net Code of my form</p>
  </blockquote>
  
  <pre><code>Imports System.IO
  Imports Microsoft.Win32
  Imports System.Security.Permissions
  
  &lt;PermissionSet(SecurityAction.Demand, Name:="FullTrust")&gt; _
  &lt;System.Runtime.InteropServices.ComVisibleAttribute(True)&gt; _
  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
  </code></pre>