ARTICLE

Display a Web Page in a Windows Forms

Posted by Ashish Singhal Articles | Windows Forms C# January 31, 2006
In this article, we will see how we can display a web page on Windows Forms.
Reader Level:

Here in this article, we will see how we can open a web page in a Windows Forms.

To open web page we need to add webbrowser control on the windows form.

Go to toolbox and right click on that, you will see text "add/remove items",  click on that will open a dialog having .NET and Com components, click on com tab and select Microsoft web browser, after selection you will see control in toolbox then you can use similar as we use other controls, simply drag and drop on page.

locatewebbrowser.gif

Fig. 1

using System;
using System.Windows.Forms;
namespace OpenBrowser
{

          public class WebPage : System.Windows.Forms.Form

          {

 

                    private AxSHDocVw.AxWebBrowser axWebBrowser1;

                    private System.Windows.Forms.Button browseButton;

 

                    public WebPage ()

                    {

                             InitializeComponent();

                    }

 

                    private void InitializeComponent()

                    {

                             System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(WebPage));

                             this.browseButton = new System.Windows.Forms.Button();

                             this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();

                             ((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();

                             this.SuspendLayout();

 

                             //

                             // browseButton

                             //

 

                             this.browseButton.Location = new System.Drawing.Point(8, 8);

                             this.browseButton.Name = "browseButton";

                             this.browseButton.TabIndex = 1;

                             this.browseButton.Text = "Browse";

                             this.browseButton.Click += new System.EventHandler(this.browseButton_Click);

 

                             //

                             // axWebBrowser1

                             //

 

                             this.axWebBrowser1.Enabled = true;

                             this.axWebBrowser1.Location = new System.Drawing.Point(10, 40);

                             this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));

                             this.axWebBrowser1.Size = new System.Drawing.Size(980, 808);

                             this.axWebBrowser1.TabIndex = 6;

 

                             //

                             // WebPage

                             //

 

                             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

                             this.ClientSize = new System.Drawing.Size(292, 266);

                             this.Controls.Add(this.browseButton);

                             this.Controls.Add(this.axWebBrowser1);

                             this.Name = "WebPage";

                             this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

                             ((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();

                             this.ResumeLayout(false);

                    }

 

                    /// <summary>

                    /// The main entry point for the application.

                    /// </summary>

 

                    [STAThread]

                    static void Main()

                    {

                             Application.Run(new WebPage ());

                    }

                    private void browseButton_Click(object sender, System.EventArgs e)

                    {

                             // Use Following code with version 2.0

                             axWebBrowser1.Navigate(http:www.c-sharpcorner.com);
                             // Use Following code with version 1.1

                             //System.Object nullObject = 0;
                             //string str = "";
                             //System.Object nullObjStr = str;
                             //Cursor.Current = Cursors.WaitCursor;
                             //axWebBrowser1.Navigate(@"http://www.c-sharpcorner.com/",ref nullObject, ref
                             nullObjStr, ref nullObjStr, ref nullObjStr);
                             //Cursor.Current = Cursors.Default;

                             }

          }

}

COMMENT USING