2

With a lot of digging I have managed to share session between my 2 forms with the SHDocVw.dll. I have a Main form and a Popup form, each contain a WebBrowser control.

My question is : how can I handle the window.close javascript event to close the popup form ?

I think I have the correct syntax but I can't make it happen.

Main Form (webbrowser Navigator) :

    SHDocVw.WebBrowser nativeBrowser;
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        nativeBrowser = (SHDocVw.WebBrowser)Navigator.ActiveXInstance;
        nativeBrowser.NewWindow2 += nativeBrowser_NewWindow2;
    }

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        nativeBrowser.NewWindow2 -= nativeBrowser_NewWindow2;
        base.OnFormClosing(e);
    }

    void nativeBrowser_NewWindow2(ref object ppDisp, ref bool Cancel)
    {
        var popup = new Form3();
        popup.Show(this);
        ppDisp = popup.webBrowser1.ActiveXInstance;
    }

Popup form

public Form3()
    {
        InitializeComponent();         
    }

    public WebBrowser Browser
    {
        get { return webBrowser1; }
    }

I think I have to add something like this to the popup code :

SHDocVw.WebBrowser nativeBrowser;

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    nativeBrowser = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
    nativeBrowser.WindowClosing += nativeBrowser_closing;
}

protected void nativeBrowser_closing(bool IsChildWindow, ref bool Cancel)
{
    this.Close();
}

But when I declare the SHDocVw.WebBrowser nativeBrowser it opens a new IE instance instead of binding to the current one.

Can someone help me please :) ?

CptNapalm
  • 21
  • 2
  • You are doing this backwards. The DOM's close event will only be raised when you close the browser. You already know when that happens, your form's FormClosed event is raised first. – Hans Passant Sep 06 '12 at 13:54
  • This worked for me. Try this post on stackoverflow - https://stackoverflow.com/questions/5535575/webbrowser-and-javascript-window-close – Ishwar Sep 13 '19 at 14:54

0 Answers0