Search code examples
c#.netwinformsbuttonclick

open a web page on button click in form


I want show a web page (google) when i click on the button in form(winform)....

I have tried the below code but it does not work for me.....

   public partial class Form1 : Form {
    bool mHooked;
    public Form1() {
        InitializeComponent();
        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
        webBrowser1.Navigate("http://www.google.com");
    }

    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
        if (mHooked) return;
        // Get the form
        HtmlDocument doc = webBrowser1.Document;
        HtmlElement form = doc.Forms["f"];
        // Get the "I'm feeling lucky" button
        HtmlElement lucky = form.All["btnI"];
        lucky.Click += lucky_Click;
        mHooked = true;
    }
    void lucky_Click(object sender, EventArgs e) {
        this.Close();
    }
}

I am doing winforms application using c#

would any one pls help on this.....

many thanks in advance...


Solution

  • First Add a button to your form and in the Click event handler do this

    private void button1_Click(object sender, EventArgs e)
    {           
       //remove this from the constructor else it will be loaded along with the form
        webBrowser1.Navigate("http://www.google.com");
    }