我有一个名为webBrowser3的webBrowser,它显示了HTML文件内容的链接。示例: - 一个WebBrowser(webBrowser3)显示目录(TOC),我想要在我单击的任何章节或其中,它在另一个webBrowser(webBrowser2)上显示其内容,该webBrowser放置在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" height="2em">
<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;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;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>
答案 0 :(得分: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 :(得分:0)
这是我的表单的完整代码,我使用了私有变量“nexturl”,因为当我点击webbrowser3上的链接时,webbrowser2似乎没有检测到网址已经改变(链接是相同的,更改只有书签)。
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"));
}
答案 2 :(得分:0)
我的代码和您的代码之间的区别在于您不使用网址加载页面,而是直接在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);
}