C#如何让网络浏览器自动加载网址

时间:2019-06-02 01:01:02

标签: c# winforms

我正在尝试加载一个Web查看器,该程序在程序启动时会自动加载一个url。

我尝试过:

this.webBrowser1.Navigate("http://www.microsoft.com");

一些代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            this.webBrowser1.Navigate("http://www.microsoft.com");

        }
    }
}

我希望当Brogram运行时,Web查看器会自动转到URL。

1 个答案:

答案 0 :(得分:1)

Form1_Load内尝试此代码

// for initial loading of the page this doesn't have to be true, this is only for navigation after loading the 1st page
webBrowser1.AllowNavigation = true; 

webBrowser1.DocumentCompleted += new 
          WebBrowserDocumentCompletedEventHandler(WebBrowser1_DocumentCompleted);

webBrowser1.Navigate("http://www.microsoft.com");