How to use the .net webBrowser object

最后都变了- 提交于 2019-12-23 10:26:27

问题


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


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/5716662/how-to-use-the-net-webbrowser-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!