5

any one know of a tutorial for using the System.Windows.Forms.WebBrowser object? Had a look around but can't find one. The code I have so far is (the very complex):

System.Windows.Forms.WebBrowser b = new System.Windows.Forms.WebBrowser();
b.Navigate("http://www.google.co.uk");

but it doesn't actually navigate anywhere (i.e. b.Url is null, b.Document is null etc)

Thanks

Patrick
  • 8,175
  • 7
  • 56
  • 72

5 Answers5

5

It takes time for the browser to navigate to a page. The Navigate() method does not block until the navigation is complete, that would freeze the user interface. The DocumentCompleted event is fired when it's done. You have to move your code into an event handler for that event.

An additional requirement is that the thread on which you create a WB is a happy home for single-threaded COM components. It must be STA and pump a message loop. A console mode app does not meet this requirement, only a Winforms or WPF project has such a thread. Check this answer for a solution that's compatible with console mode programs.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

It is very simple control. Use Following Code

    // Navigates to the URL in the address box when 
// the ENTER key is pressed while the ToolStripTextBox has focus.
private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Navigate(toolStripTextBox1.Text);
    }
}

// Navigates to the URL in the address box when 
// the Go button is clicked.
private void goButton_Click(object sender, EventArgs e)
{
    Navigate(toolStripTextBox1.Text);
}

// Navigates to the given URL if it is valid.
private void Navigate(String address)
{
    if (String.IsNullOrEmpty(address)) return;
    if (address.Equals("about:blank")) return;
    if (!address.StartsWith("http://") &&
        !address.StartsWith("https://"))
    {
        address = "http://" + address;
    }
    try
    {
        webBrowser1.Navigate(new Uri(address));
    }
    catch (System.UriFormatException)
    {
        return;
    }
}

// Updates the URL in TextBoxAddress upon navigation.
private void webBrowser1_Navigated(object sender,
    WebBrowserNavigatedEventArgs e)
{
    toolStripTextBox1.Text = webBrowser1.Url.ToString();
}

You can also use this example

Extended Web Browser

Abhi
  • 5,501
  • 17
  • 78
  • 133
0

Drop a webbrowser control to a form and set its AllowNavigation to true. Then add a button control and in its click event, write webBrowser.Navigate("http://www.google.co.uk") and wait for the page to load.

For a quick sample you can also use webBrowser.DocumentText = "<html><title>Test Page</title><body><h1> Test Page </h1></body></html>". This will show you sample page.

Vijayakumar
  • 51
  • 1
  • 9
0

Try this simple example
Declaration:

using System.Windows.Forms;

Usage:

WebBrowser b = new WebBrowser();
b.DocumentCompleted += ( sender, e ) => {
    WebBrowser brw = ( WebBrowser )sender;
    // brw.Document should not null here
    // Do anythings with your Document
    HtmlElement div = brw.Document.GetElementById( "my_div" );
    head.InnerHtml = "Hello World";
    //brw.Url should not null here
    Console.WriteLine( brw.Url.AbsoluteUri );
    // Invoke JS function
    brw.Document.InvokeScript( "any_global_function", new object[] { "From C#" } );
};
b.Navigate("http://www.google.co.uk");
Rajib Chy
  • 800
  • 10
  • 22
-3

If you are just trying to open a browser and navigate I am doing it very basic, everyone's answer is very complex.. I am very new to c# (1 week in) and I just did this code:

string URL = "http://google.com";
object browser; 
browser = System.Diagnostics.Process.Start("iexplore.exe", URL)

//This opens a new browser and navigates to the site of your URL variable
Jeff
  • 13
  • 3
    This does not answer the question about `System.Windows.Forms.WebBrowser` – Kevin Panko Oct 15 '13 at 21:41
  • For wasting time reading it. This does not use the WebBrowser control, intended on the question. – fcm Mar 24 '16 at 19:46
  • @fcm Alternative answers can be helpful to people who may appreciate a suggestion that takes them outside the box. – AnthonyVO Jun 17 '17 at 14:34
  • @AnthonyVO To show how to start IE on a process does not help at all. System.Windows.Forms.WebBrowser allows you to automate, read the downloaded document and multitude of other stuff; To start an independent process does nothing not even close to that; and, if I want to learn how to start a process, I will not look here. – fcm Jun 18 '17 at 16:03