Microsoft KB Archive/311282

= How to use the WebBrowser control NewWindow2 event in Visual Basic .NET =

Article ID: 311282

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Internet Explorer 5.5
 * Microsoft Internet Explorer (Programming) 5.5 SP2
 * Microsoft Internet Explorer (Programming) 6.0

-



This article was previously published under Q311282





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

IN THIS TASK

 * SUMMARY
 * The NewWindow2 event
 * Create the project and then add code
 * Verification
 * REFERENCES



SUMMARY
This article describes how to use the NewWindow2 event. The NewWindow2 event is triggered by the WebBrowser control that is included with Microsoft Internet Explorer 4.0 and later versions. You can use this event to specify that your browser program will always be used when a new browser window is opened. This article describes this procedure by using Visual Basic .NET.

back to the top

The NewWindow2 event
The NewWindow2 event occurs when a new window is to be created for displaying a resource. This event occurs before a new window is created from the WebBrowser control. For example, this event occurs 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 to be used when a new window is opened, set the ppDisp parameter equal to a new WebBrowser object that is contained in a new window that your program creates. 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. This setting causes the new WebBrowser 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 examines all the existing window names before opening a new window.

back to the top

Create the project and then add code
The following sample navigates the WebBrowser control to http://www.microsoft.com.  Start Microsoft Visual Studio .NET. Create a new Windows Application project in Visual Basic .NET. In the toolbox, click General. Right-click the open panel, and then click Customize Toolbox. Select the Microsoft Web Browser check box, and then click OK. In the toolbox, double-click Explorer.</li> Add a button and a text box to your form.</li>  Double-click the button to view the implementation of the Click event of the Button1 button in the code window. Append the following code to the existing code. This code lets you move to the URL that is specified in the text box. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click AxWebBrowser1.Navigate(TextBox1.Text) End Sub Write the handler function for the AxWebBrowser1.NewWindow2 event as follows. Private Sub AxWebBrowser1_NewWindow2(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NewWindow2Event) Handles AxWebBrowser1.NewWindow2 Dim frmWB As Form1 frmWB = New Form1

frmWB.AxWebBrowser1.RegisterAsBrowser = True e.ppDisp = frmWB.AxWebBrowser1.Application frmWB.Visible = True End Sub </li></ol>

back to the top

Verification
<ol> Build the program.</li>  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> Run the program. The Form1 form appears.</li> Type the path of the Test.htm file in the box, and then click Button1. The Open New Window button appears.</li> Click Open New Window. Notice that the Microsoft Web page opens in a new instance of the program.</li></ol>

back to the top

<div class="references_section">