C# 显示打印预览对话框后何时/如何处置Web浏览器控件

C# 显示打印预览对话框后何时/如何处置Web浏览器控件,c#,winforms,C#,Winforms,我正在windows窗体项目中创建一个新的Web浏览器控件,以打印一些格式化的HTML。用户不需要看到控件,因为它只是打印他们所在页面的特殊格式版本 这是我目前正在使用的打印过程 internal void PrintQuestion(SessionPart SessionPart) { WebBrowser wbForPrinting = new WebBrowser(); //<-- Undisposed local wbForPrint

我正在windows窗体项目中创建一个新的Web浏览器控件,以打印一些格式化的HTML。用户不需要看到控件,因为它只是打印他们所在页面的特殊格式版本

这是我目前正在使用的打印过程

    internal void PrintQuestion(SessionPart SessionPart)
    {
        WebBrowser wbForPrinting = new WebBrowser(); //<-- Undisposed local
        wbForPrinting.Parent = this;
        wbForPrinting.DocumentCompleted += wbForPrinting_DocumentCompleted;
        wbForPrinting.DocumentText = string.Format(DOCUMENT_HTML, SessionPart.Session.Course.Product.Name, HtmlFormatter.GetPrintableQuestion(SessionPart));
    }

    void wbForPrinting_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        // Print the document now that it is fully loaded.
        ((WebBrowser)sender).ShowPrintPreviewDialog();
    }
内部无效打印问题(SessionPart SessionPart)
{
WebBrowser wbForPrinting=新WebBrowser();//您尝试过这个吗

void wbForPrinting_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // Print the document now that it is fully loaded.
    ((WebBrowser)sender).ShowPrintPreviewDialog();
    // Dispose the WebBrowser now that the task is complete. 
    ((WebBrowser)sender).Dispose();
}

您想清除缓存..还是处置..您看过编组..吗?我正在尝试处置WebBrowser。不,我没有看过编组,不确定那是什么。是的,这是我尝试的第一件事,正如问题中提到的"我无法在“显示打印预览”对话框之后立即处理WebBrowser,因为这会破坏我正在尝试打印的对象。您是否尝试过我在答案中发布的链接中的示例?单击“使用Web浏览器打印”控件,然后处理..当要打印的文档加载..时tep-thru代码正在发生的事情..?也将您的代码包装在一个更容易理解的Try{}Catch{}周围..如果您有一个异常,可能是因为什么原因,可能是I-D-10-T错误,在我发布问题之前,此方法不起作用。现在它按预期工作。太棒了..很高兴我能帮忙。