Search code examples
vb.netwinformscefsharp

CefSharp inside a Control


When I attempt to add CefSharp to a Panel or Groupbox it doesn't display anything (by that I mean that the webbrowser doesn't show, even a blank page, only the control background is visible as if there is nothing added).

But when I use "Me" instead, it works normally. Why is that? Is it not possible to add it to wherever you want?


Public Class Form_Main  
    Private WithEvents chromeBrowser As ChromiumWebBrowser
    Public Sub InitializeChromium()
        InitializeComponent()
        Dim CEF_settings As New CefSettings With {
            .CachePath = "Cache"
        }
        CefSharp.Cef.Initialize(CEF_settings)
        chromeBrowser = New ChromiumWebBrowser("www.google.com") With {
            .Dock = DockStyle.Fill
        }
        Panel1.Controls.Add(chromeBrowser) 'With Me.Controls.Add(chromeBrowser) it works
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        InitializeChromium()
    End Sub
End Class

Solution

  • I don't know why, but changing the Method name to "New" fixes it.

    So instead of

    Public Sub InitializeChromium()
    'Stuff"
    End Sub
    

    it must be

    Public Sub New()
    'Stuff"
    End Sub
    

    Now the webbrowser gets displayed no matter where it's placed.