Calling sub in different class not working right

270 views Asked by At

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.

0

There are 0 answers