Microsoft KB Archive/815714

= How to use the WebBrowser control NewWindow2 event in Visual C# .NET or in Visual C# 2005 =

Article ID: 815714

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Internet Explorer 5.5
 * Microsoft Internet Explorer (Programming) 5.5 SP2
 * Microsoft Internet Explorer (Programming) 6.0
 * Microsoft Visual C# 2005 Express Edition

-





For a Microsoft Visual Basic .NET version of this article, see 311282.



For a Microsoft Visual Basic 6.0 version of this article, see 184876.



IN THIS TASK

 * SUMMARY
 * The NewWindow2 Event
 * Create the Project and Add Code
 * Verification
 * REFERENCES



SUMMARY
This article describes how to use the NewWindow2 event that is triggered by the Microsoft WebBrowser control that is included with Microsoft Internet Explorer 4.0 and later. You can use this event to specify that your browser program is used whenever a new browser window is opened. This article describes this procedure for Visual C# .NET or Visual C# 2005.

back to the top

The NewWindow2 Event
The NewWindow2 event occurs when a new window is created to display a resource. This event comes before a new window is created from the WebBrowser control (for example, in response to a navigation that is targeted to a new window or to a scripted window.open method).

To specify that your browser program is used whenever a new window is opened, set ppDisp equal to a new WebBrowser object that is contained in a new window that is created by your program. In this scenario, if a user chooses to open a Web page in a new window, the new window in your program is used to display the new Web page.

Additionally, set the RegisterAsBrowser property to true for the new WebBrowser control for the control to participate in window-name resolution. For example, if the window name is used elsewhere in the script, this control is used instead of a newly created one because the control checks all the existing window names before opening a new window.

back to the top

Create the Project and Add Code
The following sample directs the WebBrowser control to the following Web site:

http://www.microsoft.com

 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. Create a new Visual C# Windows Application project. In the toolbox, click General. Right-click the open panel, and then click Customize Toolbox. Click the Microsoft Web Browser check box, and then click OK. In the toolbox, double-click Explorer control.</li> Add a button control and a text box control to your form.</li>  Double-click the button to view the implementation of the onClick event in the code window. Add the following code, so that you can navigate to the URL that is specified in the text box: private void button1_Click(object sender, System.EventArgs e) { object obj = null; axWebBrowser1.Navigate(textBox1.Text, ref obj, ref obj, ref obj, ref obj); } </li>  In Solution Explorer, right-click Form1, and then click View Designer. Right-click axWebBrowser1, and then click Properties. Click the Events tab, and then double-click NewWindow2. Add the following code to NewWindow2 event: private void axWebBrowser1_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e) { Form1 frmWB; frmWB = new Form1;

frmWB.axWebBrowser1.RegisterAsBrowser = true; e.ppDisp = frmWB.axWebBrowser1.Application; frmWB.Visible = true; } </li></ol>

back to the top

Verification
<ol>  Start Notepad, and then save the following text as Test.htm on your Web server: <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <HEAD> <META HTTP-EQUIV=&quot;Content-Type&quot; CONTENT=&quot;text/html; charset=iso-8859-1&quot;> <TITLE>Q311282</TITLE>

<SCRIPT type = &quot;text/Jscript&quot;> function openWin {         var win; win = window.open(&quot;http://www.microsoft.com&quot;); } </SCRIPT> </HEAD> <BODY>

<button onClick=&quot;openWin&quot;>Open New Window

</BODY> </HTML> </li> In Visual Studio .NET or Visual Studio 2005, on the Debug menu, click Start to run the application.</li> Form1 appears. Change the text in the text box to the URL of the Test.htm page.</li> Click button1.</li> Test.htm appears in the WebBrowser control. Click Open New Window. Note that the Microsoft Web page opens in a new instance of the program.</li></ol>

back to the top

<div class="references_section">