webbrowser控件 未将对象引用设置到对象的实例 坐等..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace C_sharp_KanCms_Banner_
{
public partial class WebPage : Form
{
public WebPage()
{
InitializeComponent();
this.webBrowser1.Navigate("www.baidu.com");
this.webBrowser1.Document.Body.Style = "zoom:0.5";//出问题处
}
}
}
[解决办法]
没有等DocumentComplete就访问Document……
[解决办法]
页面没可能立即就加载完成的。把
this.webBrowser1.Document.Body.Style = "zoom:0.5";//出问题处
放到DocumentComplete事件中。
- C# code
private void PrintHelpPage(){ // Create a WebBrowser instance. WebBrowser webBrowserForPrinting = new WebBrowser(); // Add an event handler that prints the document after it loads. webBrowserForPrinting.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(PrintDocument); // Set the Url property to load the document. webBrowserForPrinting.Url = new Uri(@"\\myshare\help.html");}private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e){ // Print the document now that it is fully loaded. ((WebBrowser)sender).Print(); // Dispose the WebBrowser now that the task is complete. ((WebBrowser)sender).Dispose();}