0

I am creating a browser in VB.Net using Cefsharp. I created a custom LifeSpanHandler to handle Popup windows, but when I try to call a Public Sub in a different class, it is not giving the expected output. I created my LifeSpanHandler using the following code:

Public Class LifeSpanHandler
Implements ILifeSpanHandler
Public Event PopupRequest As Action(Of String)
Public Function OnBeforePopup(browser As IWebBrowser, sourceUrl As String, targetUrl As String, ByRef x As Integer, ByRef y As Integer, ByRef width As Integer, ByRef height As Integer) As Boolean Implements ILifeSpanHandler.OnBeforePopup
    RaiseEvent PopupRequest(targetUrl)
    My.Settings.newpage = targetUrl
    Call Form1.IntNewTab()
    Return True
End Function
Public Sub OnBeforeClose(browser As IWebBrowser) Implements ILifeSpanHandler.OnBeforeClose

End Sub
End Class

And then I have browser.LifeSpanHandler = New LifeSpanHandler where it is Initialized. (With browser being CefSharp.WinForms.ChromiumWebBrowser)

I save the targetURL in My.Settings.newpage, then when the browser is initialized, it opens to that URL. In a different class (and a different Form), I have this code:

Public Sub IntNewTab()
    Dim tab As New TabPage
    Dim newtab As New tabs
    newtab.Show()
    newtab.Dock = DockStyle.Fill
    newtab.TopLevel = False
    tab.Controls.Add(newtab)
    Me.CustomTabControl1.TabPages.Add(tab)
    Me.PictureBox1.Location = New System.Drawing.Point(PictureBox1.Location.X + 150, 3)
    My.Settings.newpage = My.Settings.homepage
    Me.CustomTabControl1.SelectedTab = tab
End Sub

Which is the code to add a new tab. But in my LifeSpanHandler, when I Call Form1.IntNewTab(), the browser freezes out of focus. The window is grayed out (meaning it's out of focus) and I can't drag it around, and it stays on top of everything else, and I can't interact with any part of the browser.

To test something else out, I added a button to Form1 with the exact code from IntNewTab, and when I click on it, it opens a new tab to the specified page like normal. I also tried leaving the button visible, and OnBeforePopup adding Form1.Button1.PerformClick, but that did not work either. Anyone else have experience with doing this, or have any suggestions?

Edit:

I added the following codes to my browser to try and get rid of the default instance (the best I understood it):

To Form1:

Module Program
Friend frmMain As Form1
End Module

In Form1_Load:

`frmMain = Me`

Then I added this code in the LifeSpanHandler to reflect the changes:

Dim mainFrm = New Form1()
    mainFrm.IntNewTab()

And that did not work. It just kept freezing out of focus like before. I also tried adding frmMain.IntNewTab() (the code that is in Form1_Load), and it still did not work.

AJDev
  • 1,397
  • 4
  • 12
  • 22
  • Using the default form instance might be the problem. It doesnt seem like it should be, but maybe... – Ňɏssa Pøngjǣrdenlarp Jun 18 '15 at 22:14
  • @Plutonix what do you mean? Are you talking about changing Form1 to something else? – AJDev Jun 18 '15 at 22:17
  • http://stackoverflow.com/a/28290434/1070452 – Ňɏssa Pøngjǣrdenlarp Jun 18 '15 at 22:19
  • @Plutonix I understood it the best I could, and I made edits to my original question. – AJDev Jun 18 '15 at 22:36
  • You'll likely have to marshal back onto the `UI` thread. `LifeSpanHandler` is running in one of the `CEF` threads. – amaitland Jun 18 '15 at 23:12
  • @amaitland I'm sorry, but do you have an example? This is my first time working with multiple threads... – AJDev Jun 18 '15 at 23:33
  • I don't know VB.Net so can't provide you with a direct sample. `WinForms` example https://github.com/cefsharp/CefSharp/blob/79d8565d9e6ab74c914cfd1e90bf97ae2e6a4c77/CefSharp.WinForms/Internals/ControlExtensions.cs#L17 `WPF` example https://github.com/cefsharp/CefSharp/blob/801bebbbd088a2948a9a7330c4d19cb772cb9c77/CefSharp.Wpf/ChromiumWebBrowser.cs#L862 – amaitland Jun 19 '15 at 08:30

0 Answers0