RSS

C# Web Browser with Popup Blocker

07 Mar

C# Web Browser with pop-up blocker:
1). Create a new C# windows application project in Visual Studio.
2). Resize the form to 800×500 px.
3). Drag the following controls from the toolbox(press Ctrl+Alt+X to show toolbox):
a). 1 Buttons (name it as buttonGo)
b). 1 Text Boxes (name it as buttonGo)
c). 1 Microsoft Web Browser
NOTE: We’ll use the activeX web browser control and not the web browser that is directly visible in the toolbox. To get this active web browser control right click on toolbox, select “Choose Toobox Items”, goto COM Components tab, select Microsoft Web Browser and click OK.

5). Arrange all the controls to give your form the following look:

6). Button “Go” is for navigating to the address in the text box on the left to it.
7). Replace “Afact” everywhere in the code below with the name you gave to your application.

8). Write the following code to buttonGo events and NewWindow2 event of the web browser control:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Afact
{
public partial class Afact : Form
{

public Afact()
{
InitializeComponent();
}

private void Afact_Load(object sender, EventArgs e)
{
axWebBrowser1.Navigate(“www.google.com”);

}

private void buttonGo_Click(object sender, EventArgs e)
{
axWebBrowser1.Navigate(textBoxURL.Text);
}

private void axWebBrowser1_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
{
// Is document ready state complete ? If not, that’s an ad popup. Don’t allow it
if (axWebBrowser1.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
{
e.ppDisp = null;
e.cancel = true;
return;
}

/* Else let’s create a new form instance and
send the new web browser instance back to the caller.*/
Afact newwindow = new Afact();
newwindow.Text = “Afact New Window”;
e.ppDisp = newwindow.axWebBrowser1.Application;
newwindow.Show();
}

}
}

 

Download the project from the BOX on the right side!!!

 

Advertisements
 
Leave a comment

Posted by on March 7, 2011 in C#

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: