在 C# winforms 中的同一表单上从一个 webBrowser 导航到另一个 webBrowser

Posted

技术标签:

【中文标题】在 C# winforms 中的同一表单上从一个 webBrowser 导航到另一个 webBrowser【英文标题】:navigate from one webBrowser to another webBrowser on the same form in C# winforms 【发布时间】:2014-02-03 11:20:22 【问题描述】:

我有一个名为 webBrowser3 的 webBrowser,它显示指向其 html 文件内容的链接。示例:- 一个 WebBrowser(webBrowser3) 显示目录 (TOC),我希望在我单击的任何章节或其部分上,它在另一个 webBrowser(webBrowser2) 上显示其内容,该 webBrowser2 以相同的 webBrowser3 形式放置。我已经制作了目录(TOC),但是当单击任何章节时,它会在放置 TOC 的同一 webBrowser(webBrowser3)上显示其内容。 我已经通过以下方式进行了尝试,但没有显示出预期的结果。

  string toc = File.ReadAllText(StaticClass.ZipFilePath + "\\OEBPS\\IDSBooktoc.html");

         webBrowser3.DocumentText =toc;

我不知道我应该做什么才能做到这一点...... 这是我的 HTML

    <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Table of Contents</title>
    <!--Styles for the Table of Contents-->
    <style>h1 text-align: center
    p text-align: left
    p.level_0 text-indent: 0em 
    p.level_1 text-indent: 1em 
</style>
  </head>
  <body>
    <h1 class="tocHead">Table of Contents</h1>
    <p class="level_0" >
      <a href="IDSBookout.html#NCXGen0">2008-21-0043 Chapter 1 Development</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen1">INTRODUCTION 1</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen2">TRADITIONAL PRODUCT DEVELOPMENT AND VERIFICATION PROCESS</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen3">DEVELOPMENT BENCHES</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen4">ECU SYSTEM AND IT&amp;amp;V TESTING</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen5">INTRODUCTION TO THE ECU VIRTUAL PROTOTYPING [] []</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen6">VIRTUAL PRODUCT DEVELOPMENT AND VERIFICATION PROCESS</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen7">CREATION OF THE VIRTUAL TEST ENVIRONMENT</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen8">USER INTERFACE</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen9">PLANT MODEL</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen10">SIMULATION PLATFORM</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen11">CONNECTING THE VERIFICATION TEST BENCH TO THE VIRTUAL ECU</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen12">COMPARISON: TRADITIONAL TEST BENCH VS VIRTUAL TEST BENCH</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen13">Case Study One: Transmission Management System</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen14">Discrete Outputs</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen15">Discrete Inputs</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen16">BENEFITS TO DATE</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen17">FUTURE INVESTIGATION AND IMPROVEMENTS</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen18">CONCLUSIONS</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen19">ACKNOWLEDGMENTS</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen20">2008-21-0043 Chapter 2 Development</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen21">INTRODUCTION 2</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen22">TRADITIONAL PRODUCT DEVELOPMENT AND VERIFICATION PROCESS 2</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen23">DEVELOPMENT BENCHES</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen24">ECU SYSTEM AND IT&amp;amp;V TESTING</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen25">INTRODUCTION TO THE ECU VIRTUAL PROTOTYPING 3</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen26">VIRTUAL PRODUCT DEVELOPMENT AND VERIFICATION PROCESS 1</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen27">CREATION OF THE VIRTUAL TEST ENVIRONMENT 2</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen28">USER INTERFACE</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen29">PLANT MODEL</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen30">SIMULATION PLATFORM</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen31">CONNECTING THE VERIFICATION TEST BENCH TO THE VIRTUAL ECU
2</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen32">COMPARISON: TRADITIONAL TEST BENCH VS VIRTUAL TEST BENCH 2</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen33">Case Study One: Transmission Management System 2</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen34">Discrete Outputs</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen35">Discrete Inputs</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen36">BENEFITS TO DATE 2</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen37">FUTURE INVESTIGATION AND IMPROVEMENTS</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen38">CONCLUSIONS 2</a>
    </p>
    <p class="level_0">
      <a href="IDSBookout.html#NCXGen39">ACKNOWLEDGMENTS 2</a>
    </p>
  </body>
</html>

【问题讨论】:

【参考方案1】:
private string tocpage = "IDSBooktoc.html";
private string outpage = "IDSBookout.html";

private void Form1_Load(object sender, EventArgs e)

    //load toc, only for parsing
    webBrowser2.Visible = false;
    webBrowser2.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser2_DocumentCompleted);
    webBrowser2.DocumentText = File.ReadAllText(tocpage);


void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

    //Populate treeview
    foreach (HtmlElement ele in webBrowser2.Document.GetElementsByTagName("a"))
    
        treeView1.Nodes.Add(ele.GetAttribute("href"), ele.InnerText);
    
    //detach event
    webBrowser2.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(webBrowser2_DocumentCompleted);
    //change webbrowser2 source
    webBrowser2.DocumentText = File.ReadAllText(outpage);


private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)

    string selected = e.Node.Name; //IDSBookout.html#NCXGen0
    string bookmark = selected.Substring(selected.IndexOf("#") + 1);
    webBrowser2.Document.GetElementById(bookmark).ScrollIntoView(true);
    webBrowser2.Visible = true;

【讨论】:

我遇到了一些问题,我的节点在树视图中出现了两次。示例:- 我的树视图的结构是 - 简介 1 - 传统产品开发和验证过程 - 简介 1 - 传统产品开发和验证过程 但应该是 - 简介 1 - 传统产品开发和验证过程 当我删除“webBrowser2.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser2_DocumentCompleted);”然后我的问题就解决了。但我不明白为什么我们在代码中写了这行代码。你能解释一下这段代码行的用法吗? 我写这行代码是因为我首先使用 webbrowser2 来解析 toc 文件。在我的代码中,我:1)读取 html 文件 2)ondocumentcompleted 事件填充树视图 3)分离事件,因为我不需要它。也许你在另一个事件中解析你的 html? 非常感谢您的帮助。实际上,这是我的错误,我自己在触发此事件之前没有清除节点。【参考方案2】:

这是我表单的完整代码,我使用了私有变量“nexturl”,因为当我单击 webbrowser3 上的链接时,webbrowser2 似乎没有检测到 url 已更改(链接相同,请更改只有书签)。

private Uri nexturl = null;

private void Event_Navigating(object sender, WebBrowserNavigatingEventArgs e)

    e.Cancel = true;
    nexturl = e.Url;
    webBrowser2.Navigate(new Uri("about:blank"));//necessary, the webbrowser2 don't see that the url has changed


private void webBrowser3_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

    webBrowser3.Navigating += new WebBrowserNavigatingEventHandler(Event_Navigating);
    webBrowser2.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser2_DocumentCompleted);


void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

    if (nexturl != null)
    
        webBrowser2.Navigate(nexturl);
        nexturl = null;
    


private void Form1_Load(object sender, EventArgs e)

    webBrowser3.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser3_DocumentCompleted);
    webBrowser3.Navigate(new Uri("file://" + AppDomain.CurrentDomain.BaseDirectory + "IDSBooktoc.html"));

【讨论】:

在我的代码中,Event_Navigating 方法不会被调用。 在发布之前我已经在我的电脑上尝试过代码。可以提供一份html文件吗? 如果调试项目,是否调用事件webBrowser3_DocumentCompleted? 我已经添加了我的 HTML。 我希望你能得到我想做的事。在这个 html 中,我有一些链接指向另一个 HTML 文件。我已将它放在 webBrowser3 中,如果我单击此 HTML 文件中的任何链接,我想在 webBrowser2 中打开其内容。并且两个 webBrowser 都放在一个表单上。【参考方案3】:

我的代码和你的代码的区别在于你不使用url来加载页面,而是在webbrowser2和webbrowser3中直接设置html。

这样试试:

private string tocpage = "IDSBooktoc.html";
private string outpage = "IDSBookout.html";

private void Form1_Load(object sender, EventArgs e)

    webBrowser3.DocumentText = File.ReadAllText(tocpage);
    webBrowser2.DocumentText = File.ReadAllText(outpage);

    webBrowser3.Navigating += new WebBrowserNavigatingEventHandler(webBrowser3_Navigating);


private void webBrowser3_Navigating(object sender, WebBrowserNavigatingEventArgs e)

    e.Cancel = true;

    //scroll webbrowser2
    string bookmark = e.Url.Fragment.Replace("#", "");
    webBrowser2.Document.GetElementById(bookmark).ScrollIntoView(true);

【讨论】:

我还有一个问题...... 如果我正在使用树视图,那么我应该触发哪个事件才能从树视图导航到 Web 浏览器? 尝试 AfterSelect 事件 你能给我写一下treeview的代码吗?

以上是关于在 C# winforms 中的同一表单上从一个 webBrowser 导航到另一个 webBrowser的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C# Winforms 表单之间转换?

WinForms C#中自定义对象类型的跨进程拖放

WinForms 中的每个表单都有自己的线程吗?

C# winform 两个定时器访问同一个变量

C# WinForms - 在同一 TreeViewControl 中拖放

Winforms c#从弹出表单更改父表单的背景图像