System.Windows.Forms.WebBrowser open links in same window or new window with same session

28,908

Solution 1

I just spent an hour looking for the answer, so I though I would post the results here. You can use the SHDocVwCtl.WebBrowser_V1 object to capture the NewWindow event.

NOTE: Code from http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_21484555.html#discussion

//-------------------------------VB.NET Version:-------------------------------

Dim WithEvents Web_V1 As SHDocVwCtl.WebBrowser_V1

Private Sub Form_Load()
    Set Web_V1 = WebBrowser1.Object
End Sub

Private Sub Web_V1_NewWindow(ByVal URL As String, ByVal Flags As Long, ByVal TargetFrameName As String, PostData As Variant, ByVal Headers As String, Processed As Boolean)
    Processed = True
    WebBrowser1.Navigate URL
End Sub


//-------------------------------C# Version-------------------------------

private SHDocVw.WebBrowser_V1 Web_V1; //Interface to expose ActiveX methods

private void Form1_Load(object sender, EventArgs e)
{
    //Setup Web_V1 interface and register event handler
    Web_V1 = (SHDocVw.WebBrowser_V1)this.webBrowser1.ActiveXInstance;
    Web_V1.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(Web_V1_NewWindow);
}

private void Web_V1_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData,string Headers, ref bool Processed)
{
    Processed = true; //Stop event from being processed

    //Code to open in same window
    this.webBrowser1.Navigate(URL);

    //Code to open in new window instead of same window
    //Form1 Popup = new Form1();
    //Popup.webBrowser1.Navigate(URL);
    //Popup.Show();
}

Solution 2

Slightly cleaned up version of Greg's answer. It modifies the passed-in control's behavior rather than relying on a global variable. Usage:

InlinePopups(webBrowser1);

Code:

// interface to expose ActiveX methods
private SHDocVw.WebBrowser_V1 Web_V1;
private void InlinePopups(WebBrowser browser)
{
    // hooks to force new windows to open in the current instance
    Web_V1 = (SHDocVw.WebBrowser_V1)browser.ActiveXInstance;
    Web_V1.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler((string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed) =>
    {
        Processed = true; // stop event from being processed

        // open in the existing window
        browser.Navigate(URL);
    });
}

Still needs the reference to %WINDIR%\system32\shdocvw.dll, of course.

Solution 3

After adding the reference to shdocvw.dll to your project if you are not adding the actuasl object to your toolbox (shwos as "Microsoft Browser") then define the object at the top of your code with:

Dim WithEvents Web_V1 As SHDocVw.WebBrowser_V1

Share:
28,908
Greg Bray
Author by

Greg Bray

Google Cloud Engineer, Retail Select Team Formerly: SRE for reddit.com, Torbit Edge Platform/CDN team @WalmartLabs, Site Reliability Engineer at Stack Overflow, Software Engineer at 3M HIS/Caradigm/GE Healthcare Twitter: @GBrayUT CodeBlog: http://codeblog.theg2.net Blog: http://blog.theg2.net About Me: http://theg2.net Resume: https://stackoverflow.com/cv/gregbray

Updated on July 05, 2022

Comments

  • Greg Bray
    Greg Bray about 2 years

    When using the .NET WebBrowser control how do you open a link in a new window using the the same session (ie.. do not start a new ASP.NET session on the server), or how do you capture the new window event to open the URL in the same WebBrowser control?

  • Asbjørn Ulsberg
    Asbjørn Ulsberg over 14 years
    In what assembly is SHDocVw defined? I can't find it in mshtml.dll at least.
  • Greg Bray
    Greg Bray over 14 years
    I think it is located at %WINDIR%\system32\shdocvw.dll
  • Admin
    Admin over 13 years
    Obejet is not a member of webBrower1 that is what I am gettinf
  • Paul Draper
    Paul Draper over 11 years
    +100 for all the worthless crap I had to read through before I found this.
  • robross0606
    robross0606 almost 7 years
    Anyone know how (or if) this can be done via Powershell?