Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

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.

Create the project and then add code

The following sample navigates the WebBrowser control to http://www.microsoft.com.
  1. Start Microsoft Visual Studio .NET.
  2. Create a new Windows Application project in Visual Basic .NET.
  3. In the toolbox, click General.
  4. Right-click the open panel, and then click Customize Toolbox.
  5. Select the Microsoft Web Browser check box, and then click OK.
  6. In the toolbox, double-click Explorer.
  7. Add a button and a text box to your form.
  8. 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
    					

Verification

  1. Build the program.
  2. Start Notepad, and then save the following text as Test.htm on your Web server.
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    <TITLE>Q311282</TITLE>
    
    <SCRIPT type = "text/Jscript">
         function openWin()
         {
              var win;
              win = window.open("http://www.microsoft.com");
         }
    </SCRIPT>
    </HEAD>
    <BODY>
    
    <button onClick="openWin()">Open New Window</button>
    
    </BODY>
    </HTML>
    					
  3. Run the program. The Form1 form appears.
  4. Type the path of the Test.htm file in the box, and then click Button1. The Open New Window button appears.
  5. Click Open New Window. Notice that the Microsoft Web page opens in a new instance of the program.

↑ Back to the top


References

For more information about the WebBrowser control and the methods, the properties, and the events that it exposes, view the WebBrowser documentation at the following Microsoft Developer Network (MSDN) Web site:

↑ Back to the top


Keywords: KB311282, kbhowtomaster

↑ Back to the top

Article Info
Article ID : 311282
Revision : 5
Created on : 5/13/2007
Published on : 5/13/2007
Exists online : False
Views : 440