【问题标题】:vb.net how to add and load an html file from resource into webbrowser controlvb.net 如何将 html 文件从资源添加并加载到 webbrowser 控件中
【发布时间】:2016-03-03 16:45:10
【问题描述】:

我在form2 上有一个webbrowser 控件,我想做的事情是:

  1. 如何将 HTML 文件添加到资源中?
  2. 当 if 条件为true 时,如何将其从资源加载到webbrowser 控件,并使其在webbrowser 中可见?

例如:

 If dttest.Rows(0).Item(0).ToString = TextBox1.Text Then
  webbrowser.navigate(resources and the html name page)
 End If    

我已经尝试过webbrowser.navigate,但我想加载 HTML 页面。

【问题讨论】:

  • 那么你的html文件是资源吗?
  • 是的,我想通过资源将一个或多个文件放入程序中,并在 if 为真时将其加载到 webbrowser 控件中。
  • 好的,谢谢你的澄清。

标签: html vb.net resources webbrowser-control


【解决方案1】:

这项工作是为我准备的,并且已经过试验和测试...我将If 条件排除在外,因为它与您要完成的工作无关。也无需调用Navigate,因为当您设置Uri 时,它应该会加载文档。

  Dim filename As String = "test.html"
  With Me.WebBrowser1 'Change name to reflect your name
      .ScriptErrorsSuppressed = True 'I was getting script errors from site
      .Url = New Uri(String.Format("file:///{0}{1}{2}/", System.IO.Directory.GetCurrentDirectory(), "/Resources/", filename))
  End With

注意

如果您的html 文件(文件)的Copy to Output Directory 未设置为Copy Always,则您需要进行更改。您可以右键单击html 文件并选择属性。根据需要更改该属性。

【讨论】:

  • 如果我理解这部分,.("file:///{0}{1}{2}/", 就像一个或多个文件的向量对吗?如果我想把只有 1 我需要用 0 写“file:///{0}”?
  • 正常情况下,当我将文件加载到项目的资源中时,它会给我一个文本文件?我在想,当我将名称放在文件名中时,网络浏览器会解码插入到该文本文件中的 html 吗?
  • 可以用图片离线网页吗?像一个完整的页面,但离线。
  • 现在我测试了代码,当如果为真,它会在 webbrowser 上显示文件夹的元素,其中的 exe 而不是在其上打开的 html 文件
  • @Axel mine 也显示为文本文件,但它具有 .html 扩展名。在 Chrome 中,您可以保存完整的网页,但某些元素在离线时不会显示;试过这个,大多数图像等都显示得很好。
猜你喜欢
  • 2015-03-30
  • 1970-01-01
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
  • 2020-04-14
  • 1970-01-01
  • 2015-03-14
相关资源
最近更新 更多