Basic C# Web Browser in Visual Studio 2008
This is a simple web browser I made in C# using visual studios 2008
First off start a new "Windows Forms Application" in visual studio.
The controls you'll need are:
Control-------Name of control------Text Displayed
ToolStrip toolStrip (none) -button1 backButton Back -button2 forwardButton Forward -separator separator (none) -label urlLabel URL: -textBox urlTextBox *Starting Url* http://www.google.com/ -button3 goButton GO ToolStrip2 statusStrip (none) -label1 doneLabel Done -label2 openingLabel Opening -progressBar progressBar (none) WebBrowser webBrowser (none)
ToolStrip
To add tools to your tool strip, if you don't know how, you simple click the drop down arrow to right of the repeat image on the toolstrip, and select the tool you want.
Go into the urlTextBox properties and set the Width of the textbox to 400.
If you have an image for you backButton, forwardButton, or goButton then go a head and use it, but if you don't just go in to the DisplayStyle Property and select Text. This should display what typed int the Text property.
Set the Enabled Property to false for both the backButton and the forwardButton.
This is what your toolStip Should look like:
(I Changed the Background color of the toolStrip from its defualt color for this tutorial so that its easyer to see)
StatusStrip
All you need to do right now is change the Alignment Property of the progressBar to Right, and set the Visible
Property of the doneLabel, openingLabel, and progressBar to False.
This is what your statusStip Should look like:
(I Changed the Background color of the statusStrip from its defualt color for this tutorial so that its easyer to see)
WebBrowser
The webBrowser's Dock Property should have been Fill by default but if its not then change it to Fill.
The Code
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 WebBrowser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Declared Variables
private string[] SiteMemoryArray = new string[100];
private int count = 0;
// Page Load
private void Form1_Load(object sender, EventArgs e)
{
webBrowser.Navigate("http://www.google.com/");// Goes To A Preset Site At Run Time
SiteMemoryArray[count] = urlTextBox.Text;// Saves URL To Memory
}
// Code For The ToolStrip
// URL TextBox
private void urlTextBox_Click(object sender, EventArgs e)
{
urlTextBox.SelectAll();// Selects All The Text In The urlTexBox
}
// GO Button
private void goButton_Click(object sender, EventArgs e)
{
webBrowser.Navigate(urlTextBox.Text);// Navigates To The Site Typed In The urlTextBox
}
// Back Button
private void backButton_Click(object sender, EventArgs e)
{
if (count > 0)// Checks To Make Sure The Count Variable Is More Then 0
{
count = count - 1;// Subtracts 1 From Count Variable
urlTextBox.Text = SiteMemoryArray[count];// Replace The Text In The urlTextBox With
// The Last URl
webBrowser.Navigate(urlTextBox.Text);// Navigates To The Site Typed In The urlTextBox
forwardButton.Enabled = true;// Enables The forwarButton
}
}
// Forward Button
private void forwardButton_Click(object sender, EventArgs e)
{
if (count < 100)// Checks To Make Sure The Count Variable Is Less Then 100
{
count = count + 1;// Adds 1 To Count Variable
urlTextBox.Text = SiteMemoryArray[count];// Replace The Text In The urlTextBox With
// The Next URl
webBrowser.Navigate(urlTextBox.Text);// Navigates To The Site Typed In The urlTextBox
backButton.Enabled = true;// Enables The backButton
count = count + 1;// Adds 1 To Count Variable
// Checks To See If The Next Variable In The SiteMemoryArray Is Null
if (SiteMemoryArray[count] == null)
{
forwardButton.Enabled = false;// Disables The forwarButton
}
count = count - 1;Subtracts 1 From Count Variable
}
}
// Web Browser
// Before Navigating
private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
doneLabel.Visible = false;// Sets The Visible Property of The donLabel To False
progressBar.Visible = true;// Sets The Visible Property of The progressBar To True
progressBar.Value = 0;// Sets Value Of The progressBar to 0
progressBar.Minimum = 0;// Sets The Minimum Value The progressBar to 0
progressBar.Maximum = 100;// Sets The Maximum Value The progressBar to 100
progressBar.Step = 10;// Sets The Step Value The progressBar to 10
progressBar.PerformStep();// Preforms The Step Rasing The progressBar Value From 0 to 10
}
// After Navigating
private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
progressBar.Step = 80;// Sets The Step Value The progressBar to 80
progressBar.PerformStep();// Preforms The Step Rasing The progressBar Value From 10 to 90
urlTextBox.Text = webBrowser.Url.ToString();// Replace The Text In The urlTextBox
// With The URl Currently Loading
openingLabel.Text = "Openning: " + urlTextBox.Text;// Displays The Message Opening: And
// The Current URL
openingLabel.Visible = true;// Sets The Visible Property of The openingLabel To True
}
// After The Site Has Finished Loading
private void webBrowser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
progressBar.Step = 10;// Sets The Step Value The progressBar to 10
progressBar.PerformStep();// Preforms The Step Rasing The progressBar Value From 90 to 100
progressBar.Visible = false;// Sets The Visible Property of The progressBar To False
openingLabel.Visible = false;// Sets The Visible Property of The openingLabel To False
doneLabel.Visible = true;// Sets The Visible Property of The doneLabel To True
// Checks To Make Sure The Same URL Is Not Being Enter Twice In A Row
if (SiteMemoryArray[count].ToString() != webBrowser.Url.ToString())
{
count = count + 1;// Adds 1 To Count Variable
SiteMemoryArray[count] = urlTextBox.Text;// Saves The Site URL To Memory
}
if (count > 0)// Checks To See If The Count Is More Then 0
{
backButton.Enabled = true;// Enables The backButton
}
else
{
backButton.Enabled = false;// Disables The backButton
}
}
}
}
References
http://support.microsoft.com/kb/313068/ - Explans Some basic of the webBrowser control
http://msdn.microsoft.com/en-us/library/t9fzsyec(VS.85).aspx - Basics of The Progress Bar