tags:

views:

1881

answers:

3

My C# application includes an embedded web browser, which is made possible by the System.Windows.Forms.WebBrowser class. Users are able to navigate to websites using the app, however, when they encounter a page that includes a pop-up window, the pop-up window is opened by Internet Explorer.

Does anyone know how to suppress that behavior?

NOTE: I'm new to C#, so please take that into consideration when responding.

A: 

The WebBrowser control takes its behavior from settings. So by turning "block pop ups" in your IE settings or tweaking the following Registry key you should be able to disable this behavior.

HKLM\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE WEBOC POPUPMANAGEMENT

James D
Good response, but that would tweak the behavior of the user's primary browser. Not an option in this case.
Huuuze
+1  A: 

IIRC you can trap the NewWindow2 event on the WebBrowser control and set Cancel = true to prevent the pop-up.

This article may help:

http://www.codeproject.com/KB/cpp/ExtendedWebBrowser.aspx#GoalBlock

Kramii
+2  A: 

Are you looking to actively block popups or handle them in your application? If you're wanting to customize the blocking, then you'll have to implement the DWebBrowserEvents2 interface, specifically the NewWindow3 method. NewWindow3 method has specific functionality for blocking window popups (i.e. setting the Cancel parameter to true). These methods will also let you show your own window if you wish, though you'll have to provide your own Form to host yet another WebBrowser.

If you'd like to see some real C# source code providing advanced functionality with the WebBrowser control, I'd have to say that this article on CodeProject provided almost everything I know about the WebBrowser control. Be sure to download the source!

@Kramii is correct that you can also use the NewWindow2 event to prevent the popup. NewWindow3 provides additional parameters for if you're looking to inspect the URL or other data about the navigate to actually sometimes block and sometimes handle the popup yourself.

David Mohundro