RSS

C# Web Browser

06 Mar
Web Browser was the first project I made in C#.NET. It was the the easiest best thing I could do!
So I decided this to be the first post on myjuet. Hope you’ll like it

C# Web Browser:

1). Create a new C# windows application project in Visual Studio.
2). Resize the form to 800×500 px. And rename it as FormBrowser.
3). Drag the following controls from the toolbox(press Ctrl+Alt+X to show toolbox):
a). 13 Buttons
b). 2 Text Boxes
c). 1 Progress Bar
d). 1 Web Browser Control
e). 4 Labels
4.) Rename the controls as follows:
button1—>buttonGo
button2—>buttonBack
button3—>buttonForward
button4—>buttonCancel
button5—>buttonRefresh
button6—>buttonHome
button7—>buttonHtml
button8—>buttonPrint
button9—>buttonPrintSettings
button10—>buttonSave
button11—>buttonSettings
button12—>buttonWeb
button13—>buttonImageSearch
textBox1—>textBoxURL
textBox1—>textBoxSearch
label1—>labelStatus

Don’t rename any other control.

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

6). “<“, “>” stand for Go Back & Go Forward.
7). “Go” is for navigating to the address in the text box on the left to it.
8). “X” for stop loading, “Refresh” for refreshing the page, “Home” for navigating to the Home Page, etc etc.
9). Write the following code to button-click events and other functions:

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 ashKAP_Web_Browser /* DO NOT OVERWRITE YOUR PROJECT’S NAMESPACE STATEMENT WITH THIS STATEMENT IN CASE YOU COPY THIS PROGRAM */
{
public partial class FormBrowser : Form
{
string url;
public FormBrowser()
{
InitializeComponent();

}
private void FormBrowser_Load(object sender, EventArgs e)
{
webBrowser1.ScriptErrorsSuppressed = true;
progressBar1.Value = 0;
webBrowser1.Navigate(“www.google.com”);
}

private void buttonGo_Click(object sender, EventArgs e)
{
url = textBoxURL.Text;
if (!webBrowser1.IsOffline)
{
progressBar1.Value = 0;
webBrowser1.Navigate(url);
}
else
{
MessageBox.Show(“OOPS!!! ashKAP Browser’s not connected to internet X-(“, “ashKAP Browser’s OFFLINE”, MessageBoxButtons.OK, MessageBoxIcon.Error);
labelStatus.Text = “OFFLINE”;
}

}

private void buttonCancel_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
webBrowser1.Stop();
}

private void buttonRefresh_Click(object sender, EventArgs e)
{
textBoxURL.Text = “”;
progressBar1.Value = 0;
webBrowser1.Refresh();
}

private void buttonHome_Click(object sender, EventArgs e)
{
textBoxURL.Text = “”;
progressBar1.Value = 0;
webBrowser1.GoHome();
}

private void buttonForward_Click(object sender, EventArgs e)
{
if (webBrowser1.CanGoForward == true)
{
textBoxURL.Text = “”;
progressBar1.Value = 0;
webBrowser1.GoForward();
}

}

private void buttonBack_Click(object sender, EventArgs e)
{
if (webBrowser1.CanGoBack == true)
{
textBoxURL.Text = “”;
progressBar1.Value = 0;
webBrowser1.GoBack();
}
}

private void buttonHtml_Click(object sender, EventArgs e)
{
MessageBox.Show(webBrowser1.DocumentText, “HTML Code of the page”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void buttonSettings_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(“inetcpl.cpl”);
}

private void buttonPrint_Click(object sender, EventArgs e)
{
webBrowser1.Print();
webBrowser1.ShowPrintPreviewDialog();
}

private void buttonPrintSetup_Click(object sender, EventArgs e)
{
printDialog1.ShowDialog();
}

private void buttonWebSearch_Click(object sender, EventArgs e)
{
string sss = textBoxSearch.Text.Replace(‘ ‘, ‘+’);
webBrowser1.Navigate(“www.google.com/images?q=” + sss);
}

private void buttonSave_Click(object sender, EventArgs e)
{
webBrowser1.ShowSaveAsDialog();
}

private void buttonImageSearch_Click(object sender, EventArgs e)
{
string sss = textBoxSearch.Text.Replace(‘ ‘, ‘+’);
webBrowser1.Navigate(“www.google.com/images?q=” + sss);
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
progressBar1.Value = 100;
}


private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
progressBar1.Value += 10;
}

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
labelStatus.Text = “Done”;
}

//ADDED EXPLICITLY IN THE DESIGNER.CS FILE
void webBrowser1_StatusTextChanged(object sender, System.EventArgs e)
{
labelStatus.Text = webBrowser1.StatusText;

}
}
}

 

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

 

Advertisements
 
Leave a comment

Posted by on March 6, 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: