none
How to make a webbrowser object visible without a form? RRS feed

  • Question

  • I started a project as a console, then went to the project's properties and changed the application type to Windows Forms Applications. There's probably an easier way to do this, but it gives me a simple Windows Forms project without the form, but with a starting module.

    Anyway, in the code i have a browser. I want to make the browser visible. How do i do that? I know it can show, because it shows up during testing sometimes (i think when the javascript uses target blank). Browser.visibie = True and Browser.Show() do not seem to work:

    Imports System.Windows.Forms
    
    Module Module1
    
        Dim Browser As New WebBrowser
    
        Sub Main()
    
            Browser.Visible = True
            Browser.Show()
            Browser.Navigate("example.com")
    
            MsgBox("Show yourself, please.")
    
        End Sub
    
    End Module
    

    Monday, August 26, 2019 4:25 PM

All replies

  • You would have the problem solved already if you simply created a new Windows Forms project then copied the relevant code over. That is what I would do. I really think that your shortcut of converting the console program is the long way to do it.


    Sam Hobbs
    SimpleSamples.Info

    Monday, August 26, 2019 7:51 PM
  • The shortcut is what i use for other projects. I did it here when i started simply because i wasn't expecting to have to show the browser. I only want to now to aid debugging.

    Regardless, it can be shown without the form, and i am simply curious how to achieve that

    Monday, August 26, 2019 7:54 PM
  • Regardless, it can be shown without the form, and i am simply curious how to achieve that

    Hi

    Like this?

    If so, then this is the code to do that.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Size = New Size(700, 400)
        Location = New Point(200, 200)
        FormBorderStyle = FormBorderStyle.None
        WebBrowser1.Navigate("https://www.google.com/search?newwindow=1&q=vb.net+webbrowser&oq=vb.net+web")
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, August 26, 2019 8:37 PM
    Monday, August 26, 2019 8:36 PM
  • That url does not seem to do it, but it is a nice try. Though it looks like you're just using a form and hiding the form.

    So, i just tried using a target of _blank to see if it's the opening of a new window/tab:

    Imports System.Windows.Forms
    
    Module Module1
    
        Dim WithEvents Browser As New WebBrowser
        Private The_Web_Page_Finished_Loading As Boolean = False
    
        Sub Main()
            Browser.CreateControl()
            Browser.DocumentText = "<!DOCTYPE html><HTML Lang = ""en-US""> <HEAD><TITLE>Test</TITLE></HEAD> <BODY><A Id = ""test"" Href = ""http://example.com"" Target = ""_blank"">Click</a></BODY></HTML>"
            Wait_Until_The_Web_Page_Finishes_Loading()
            Browser.Document.GetElementById("test").InvokeMember("click")
    
        End Sub
    
        Sub Wait_Until_The_Web_Page_Finishes_Loading()
    
            Do Until The_Web_Page_Finished_Loading
                Threading.Thread.Sleep(1000)
                Application.DoEvents()
            Loop
            The_Web_Page_Finished_Loading = False
    
        End Sub
    
        Sub Page_Loaded(Sender As Object, Event_Arguments As WebBrowserDocumentCompletedEventArgs) Handles Browser.DocumentCompleted
            The_Web_Page_Finished_Loading = True
        End Sub
    
    End Module

    And sure enough, that is what does it. Hmm..


    • Edited by Brian Tkatch Monday, August 26, 2019 9:19 PM Proper example
    Monday, August 26, 2019 9:07 PM
  • Hi

    Yes, a Form without a border.

    *Do you have something against using Forms? Why are you trying to do things without a Form from within a Forms application?

    (

    Seems to me from what you have written that you may well do better by starting a new Forms project from scratch - you would probably make better headway.


    Regards Les, Livingston, Scotland

    Monday, August 26, 2019 9:24 PM
  • If what you want to do is to show a browser window the same as if you opened the URL from outside your program then there is an easy way to do that.


    Sam Hobbs
    SimpleSamples.Info

    Monday, August 26, 2019 9:37 PM
  • Ideally, nothing is to be shown. I only want to make it visible for debugging. And since i saw it could be made visible, i thought i would ask how. But for now i can just use the target trick.

    Fwiw, i'm looking to move away from IE and instead use httpclient, which will help by avoiding the savedialog entirely. Though, httpclient looks a bit daunting due to post data and cookies.




    Monday, August 26, 2019 9:37 PM
  • Thank you. I found the answer which works for debugging (which is the only time i want to show anything), and that is by using Target = "_blank" in a link. There is example code in a reply above.
    Monday, August 26, 2019 9:40 PM
  • If you really don't want any Form, you can use directly the IWebBrowser2 interface

    I just tested by converting it from .NET source and it works by setting Visible to true

    with 

        <ComImport(), Guid("0002DF01-0000-0000-C000-000000000046")>
        Public Class WebBrowser2
        End Class

    then

            Dim pWebBrowser2 As IWebBrowser2 = CType(New WebBrowser2, IWebBrowser2)
            pWebBrowser2.Navigate2("www.google.com", Nothing, Nothing, Nothing, Nothing)
            pWebBrowser2.Visible = True



    Monday, August 26, 2019 9:44 PM
  • That is awesome. :)

    I'm not quite sure how to use

        <ComImport(), Guid("0002DF01-0000-0000-C000-000000000046")>
        Public Class WebBrowser2
        End Class

    Where does that go? Or, do you have a simple example of the entire module?

    Monday, August 26, 2019 9:54 PM
  •  Or, do you have a simple example of the entire module?

    Test sample =>

    Option Strict On
    Imports System.Runtime.InteropServices
    
    Module Module1
    
        Sub Main()
            Dim pWebBrowser2 As IWebBrowser2 = CType(New WebBrowser2, IWebBrowser2)
            pWebBrowser2.Navigate2("www.google.com", Nothing, Nothing, Nothing, Nothing)
            pWebBrowser2.Visible = True
    
            Console.ReadLine()
            If (pWebBrowser2 IsNot Nothing) Then
                pWebBrowser2.Quit()
                Marshal.ReleaseComObject(pWebBrowser2)
            End If
        End Sub
    
        <ComImport(), Guid("0002DF01-0000-0000-C000-000000000046")>
        Public Class WebBrowser2
        End Class
    
        <ComImport, Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)>
        Interface IWebBrowser2
            <DispId(100)>
            Sub GoBack()
            <DispId(101)>
            Sub GoForward()
            <DispId(102)>
            Sub GoHome()
            <DispId(103)>
            Sub GoSearch()
            <DispId(104)>
            Sub Navigate(<[In]> ByVal Url As String, <[In]> ByRef flags As Object, <[In]> ByRef targetFrameName As Object, <[In]> ByRef postData As Object, <[In]> ByRef headers As Object)
            <DispId(-550)>
            Sub Refresh()
            <DispId(105)>
            Sub Refresh2(<[In]> ByRef level As Object)
            <DispId(106)>
            Sub [Stop]()
            <DispId(200)>
            ReadOnly Property Application As Object
            <DispId(201)>
            ReadOnly Property Parent As Object
            <DispId(202)>
            ReadOnly Property Container As Object
            <DispId(203)>
            ReadOnly Property Document As Object
            <DispId(204)>
            ReadOnly Property TopLevelContainer As Boolean
            <DispId(205)>
            ReadOnly Property Type As String
            <DispId(206)>
            Property Left As Integer
            <DispId(207)>
            Property Top As Integer
            <DispId(208)>
            Property Width As Integer
            <DispId(209)>
            Property Height As Integer
            <DispId(210)>
            ReadOnly Property LocationName As String
            <DispId(211)>
            ReadOnly Property LocationURL As String
            <DispId(212)>
            ReadOnly Property Busy As Boolean
            <DispId(300)>
            Sub Quit()
            <DispId(301)>
            Sub ClientToWindow(<Out> ByRef pcx As Integer, <Out> ByRef pcy As Integer)
            <DispId(302)>
            Sub PutProperty(<[In]> ByVal [property] As String, <[In]> ByVal vtValue As Object)
            <DispId(303)>
            Function GetProperty(<[In]> ByVal [property] As String) As Object
            <DispId(0)>
            ReadOnly Property Name As String
            <DispId(-515)>
            ReadOnly Property HWND As Integer
            <DispId(400)>
            ReadOnly Property FullName As String
            <DispId(401)>
            ReadOnly Property Path As String
            <DispId(402)>
            Property Visible As Boolean
            <DispId(403)>
            Property StatusBar As Boolean
            <DispId(404)>
            Property StatusText As String
            <DispId(405)>
            Property ToolBar As Integer
            <DispId(406)>
            Property MenuBar As Boolean
            <DispId(407)>
            Property FullScreen As Boolean
            <DispId(500)>
            Sub Navigate2(<[In]> ByRef URL As Object, <[In]> ByRef flags As Object, <[In]> ByRef targetFrameName As Object, <[In]> ByRef postData As Object, <[In]> ByRef headers As Object)
            <DispId(501)>
            Function QueryStatusWB(<[In]> ByVal cmdID As OLECMDID) As OLECMDF
            <DispId(502)>
            Sub ExecWB(<[In]> ByVal cmdID As OLECMDID, <[In]> ByVal cmdexecopt As OLECMDEXECOPT, ByRef pvaIn As Object, ByVal pvaOut As IntPtr)
            <DispId(503)>
            Sub ShowBrowserBar(<[In]> ByRef pvaClsid As Object, <[In]> ByRef pvarShow As Object, <[In]> ByRef pvarSize As Object)
            <DispId(-525)>
            ReadOnly Property ReadyState As System.Windows.Forms.WebBrowserReadyState
            <DispId(550)>
            Property Offline As Boolean
            <DispId(551)>
            Property Silent As Boolean
            <DispId(552)>
            Property RegisterAsBrowser As Boolean
            <DispId(553)>
            Property RegisterAsDropTarget As Boolean
            <DispId(554)>
            Property TheaterMode As Boolean
            <DispId(555)>
            Property AddressBar As Boolean
            <DispId(556)>
            Property Resizable As Boolean
        End Interface
    
        Public Enum OLECMDID
            OLECMDID_OPEN = 1
            OLECMDID_NEW = 2
            OLECMDID_SAVE = 3
            OLECMDID_SAVEAS = 4
            OLECMDID_SAVECOPYAS = 5
            OLECMDID_PRINT = 6
            OLECMDID_PRINTPREVIEW = 7
            ' to be completed...
        End Enum
    
        Public Enum OLECMDEXECOPT
            OLECMDEXECOPT_DODEFAULT = 0
            OLECMDEXECOPT_PROMPTUSER = 1
            OLECMDEXECOPT_DONTPROMPTUSER = 2
            OLECMDEXECOPT_SHOWHELP = 3
        End Enum
    
        Public Enum OLECMDF
            OLECMDF_SUPPORTED = &H1
            OLECMDF_ENABLED = &H2
            OLECMDF_LATCHED = &H4
            OLECMDF_NINCHED = &H8
            OLECMDF_INVISIBLE = &H10
            OLECMDF_DEFHIDEONCTXTMENU = &H20
        End Enum
    End Module





    • Edited by Castorix31 Monday, August 26, 2019 10:26 PM
    Monday, August 26, 2019 10:09 PM
  • That is awesome. :)

    I'm not quite sure how to use

        <ComImport(), Guid("0002DF01-0000-0000-C000-000000000046")>
        Public Class WebBrowser2
        End Class

    Where does that go? Or, do you have a simple example of the entire module?

    There is an easier way to do that but when I asked you if all you want to do is to show a browser window you indicated no, that you have a better solution. Yet using COM like that is unnecessary. You seem to insist upon doing things the hard way.



    Sam Hobbs
    SimpleSamples.Info

    Monday, August 26, 2019 10:10 PM
  • I apologize if i gave you the wrong impression. I only want it visible for debugging to figure out this error or that in the page.

    The interface here is just interesting on its own. It also might work where IE is failing anyway, that is, with the save dialog.

    Monday, August 26, 2019 10:15 PM
  • The documentation for IWebBrowser2 indicates it is deprecated:

    Note The IWebBrowser and IWebBrowserApp interfaces are deprecated.

    Are there replacements for it?



    Monday, August 26, 2019 10:17 PM
  • The documentation for IWebBrowser2 indicates it is deprecated:

    Note The IWebBrowser and IWebBrowserApp interfaces are deprecated.

    Are there replacements for it?



    It is not really deprecated as it is used by .NET

    The latest one is WebView Class (MS Edge)

    Monday, August 26, 2019 10:32 PM
  • The documentation for IWebBrowser2 indicates it is deprecated:

    Note The IWebBrowser and IWebBrowserApp interfaces are deprecated.

    Are there replacements for it?

    Of course. Use the .Net API, as in the following.

    SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
    ie.DocumentComplete += Ie_DocumentComplete;
    ie.Navigate("about:blank");
    // wait or whatever
    ie.Quit();

    You need to add a reference for SHDocVw.dll. That will use Internet Explorer, the same browser that Windows Forms uses. You do not need to use DocumentComplete unless you need to do processing after the document is loaded.



    Sam Hobbs
    SimpleSamples.Info


    Monday, August 26, 2019 11:34 PM
  • The documentation for IWebBrowser2 indicates it is deprecated:


    Of course. Use the .Net API, as in the following.

    SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();

    SHDocVw.InternetExplorer = IWebBrowser2...
    • Edited by valat Tuesday, August 27, 2019 1:18 AM
    Tuesday, August 27, 2019 1:17 AM
  • Oh and sorry, I forgot this is for VB.Net. I assume you can convert the C#code I posted to VB.Net.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, August 27, 2019 3:42 AM