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 C#


View products that this article applies to.

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#.

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.

Create the Project and Add Code

The following sample directs the WebBrowser control to the following Web site:
  1. Start Microsoft Visual Studio.
  2. Create a new Visual C# Windows Forms Application project.
  3. In the toolbox, click the General panel.
  4. Right-click the open panel, and then click Choose Items.
  5. Click the COM Components tab, click to select the Microsoft Web Browser check box, and then click OK.
  6. In the toolbox, double-click the newly added Microsoft Web Browser control to add it to your form.
  7. Add a button control and a text box control to your form.
  8. Double-click the button to view the implementation of the onClick event in the code window. Add the following code that will 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);
    }
  9. In Solution Explorer, right-click Form1, and then click View Designer. Right-click the axWebBrowser1 control, and then click Properties. Click the Events icon, and then double-click NewWindow2 to add an event handler for the NewWindow2 event. 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;
    }

Verification

  1. 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>
  2. In Visual Studio, click the Start button on the Debug menu to run the application.
  3. Form1 appears. Change the text in the text box to the URL of the Test.htm page.
  4. Click button1.
  5. 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.

↑ Back to the top


References

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

↑ Back to the top


Keywords: KB815714, kbhowtomaster, kbprogramming, kbnamespace, kbbutton, kbctrl, kbwebbrowser, kbsweptvs2008

↑ Back to the top

Article Info
Article ID : 815714
Revision : 6
Created on : 7/30/2008
Published on : 7/30/2008
Exists online : False
Views : 686