Note that there are some explanatory texts on larger screens.

plurals
  1. POGet URL for WebBrowser.NewWindow Event
    primarykey
    data
    text
    <p>I'm trying to redirect new Window event to a new tab: </p> <pre><code>myWebBrowser.NewWindow += add_NewTab; //... private void add_NewTab(object sender, CancelEventArgs e) { WebBrowser thisWebBrowser = (WebBrowser)sender; e.Cancel = true; //should block the default browser to open a new window TabPage addedTabPage = new TabPage("redirected tab"); //create a new tab tabControl_webBrowsers.TabPages.Add(addedTabPage); //add the new tab to the TabControl WebBrowser addedWebBrowser = new WebBrowser() //create the new web browser inside the new tab { Parent = addedTabPage, Dock = DockStyle.Fill }; addedWebBrowser.Navigate(thisWebBrowser.StatusText.ToString()); //set the new browser destination url } </code></pre> <p>I'm not sure that using <code>WebBrowser.StatusText</code> is the best way to obtain the new window url (this doesn't work for every site I've tested).</p> <p>Is there a better class/method to call to get new window destination instead?</p> <hr> <p><strong>UPDATE:</strong></p> <p>I've tried the solution suggested by Charlie</p> <ol> <li>added the Microsoft Internet Control (COM) reference</li> <li>added using SHDocVw;</li> <li><p>used the code:</p> <p>System.Windows.Forms.WebBrowser myWebBrowser = new System.Windows.Forms.WebBrowser(); <br> SHDocVw.WebBrowser axBrowser = (SHDocVw.WebBrowser)myWebBrowser.ActiveXInstance; <br> axBrowser.NewWindow3 += new DWebBrowserEvents2_NewWindow3EventHandler(Browser_NewWindow3);</p></li> </ol> <p>Unfortunately I've received a <code>NullReference Exception</code> on third line that I wasn't able to correct.</p> <hr> <p><strong>SOLUTION:</strong></p> <p>I don't think the solution is in the related answer (or I wasn't able to find it) because it explains how to implement the NewWindow2 event (instead of NewWindow3 which handles the original destination url) and the implementation is the same suggested here which leads to the NullReference Exception error.<br> Anyway I've discovered this two posts: </p> <ul> <li>the <a href="http://www.rapidsnail.com/Developer/topic/2008/75/3/48011/open-in-new-window-c-webbrowser-problems.aspx" rel="nofollow">discussion</a></li> <li>the <a href="http://blog.csdn.net/jinjazz/article/details/1901192" rel="nofollow">original article</a> (the page is in Chinese but the code is readable)</li> </ul> <p>The suggest is to modify the previous three lines into this one:</p> <pre><code>(myWebBrowser.ActiveXInstance as SHDocVw.WebBrowser).NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(Browser_NewWindow3); </code></pre> <p>Everything is working now and I was able to keep using the original <code>System.Windows.Forms.WebBrowser</code> in all the rest of the code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload