Click here to Skip to main content
15,894,720 members
Articles / Desktop Programming / Windows Forms

Using the WebBrowser as an extention possibility

Rate me:
Please Sign up or sign in to vote.
4.70/5 (18 votes)
22 Jun 2007BSD4 min read 86.7K   939   63  
Creating your own extentions by using a simple WebBrowser object
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CodeProject_WebBrowser_Article
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            myWebBrowser.ObjectForScripting = new WindowExternal();
        }

        private void myExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void myLoadANicePageButton_Click(object sender, EventArgs e)
        {
            // Just one little thing I keep to forget, the Enviroment.CurrentDirectory does not
            // contain a PathSeperator on the end... Stupid hu :-S
            string aFile = Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "myExtentionPage.html";

            // To load a url you just call the Navigate method.
            // There are many ways to call the Navigate method, I have used the easiest.
            // If you want to know more about this, check out the MSDN documentation...
            myWebBrowser.Navigate(aFile);
        }

        private void myCallAMethodButton_Click(object sender, EventArgs e)
        {
            // You can call any method from the page you loaded...
            // I have enclosed this call in a try-catch, because it will
            // create a exception when there isn't a page loaded ...
            try
            {
                // If you call a method, you can use a object array to
                // supply parameters. My method only has 1 parameter,
                // so I give 1 object in the array.
                myWebBrowser.Document.InvokeScript("InvokeMethod", new object[] { "Wow, impressive !" });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions