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;

                             }

          }

}

Login to add your contents and source code to this article
post comment
     

I need the form design of this code, please E-mail me....

Posted by KrANTHi Kiran KASANI Jul 05, 2011

Thanks

Posted by arun ms Jan 30, 2011

or you could use the new .Net 2.0 control System.Windows.Forms.Webbrowser.

Posted by James Jiao Aug 11, 2009
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.