vb2005 下webbrowser 控件调用窗体函数的注意事项

前端之家收集整理的这篇文章主要介绍了vb2005 下webbrowser 控件调用窗体函数的注意事项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、确保组件对象模型(COM)必须能够访问脚本对象:即使窗体对 COM 可见,将 ComVisibleAttribute 属性添加到窗体类中:
在窗体的构造函数中设置 <System.Runtime.InteropServices.ComVisibleAttribute(True)>
例:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Partial Class FormShow
2、窗体启动时,设置
webBrowser1.ObjectForScripting = me
3、确保要调用函数为 public


以下是窗体完整代码
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.ObjectForScripting = Me
End Sub

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim innerstr As String = ""
innerstr = "<html><head><style>" & ControlChars.CrLf
innerstr &= "body {font-size:12pt;}" & ControlChars.CrLf
innerstr &= "</style>" & ControlChars.CrLf
innerstr &= "<script language='vbscript'>" & ControlChars.CrLf
innerstr &= "sub clickit()" & ControlChars.CrLf
innerstr &= "ceshidiv.innertext=""zheshiyigeceshi""" & ControlChars.CrLf
innerstr &= "end sub" & ControlChars.CrLf
innerstr &= "sub testit(testindex)" & ControlChars.CrLf
innerstr &= "window.external.testit(testindex)" & ControlChars.CrLf
innerstr &= "end sub" & ControlChars.CrLf
innerstr &= "</script>" & ControlChars.CrLf
innerstr &= "</head><body>" & ControlChars.CrLf
innerstr &= "<div id='ceshidiv'> 这是一个测试</div>" & ControlChars.CrLf
innerstr &= "<input type='button' value='ceshi1' name='btn1' onclick='clickit'>" & ControlChars.CrLf
innerstr &= "<input type='button' value='ceshi2' name='btn2' onclick='testit(1)'>" & ControlChars.CrLf
innerstr &= "</body></html>"
WebBrowser1.DocumentText = innerstr
End Sub

Public Sub testit(ByVal curpage As Integer)
MsgBox("这是winform测试:" & curpage)
End Sub
End Class

4、其他在设计时候需要注意的:webBrowser1.AllowWebBrowserDrop = false '禁止拖放文件 WebBrowser 控件到其上webBrowser1.IsWebBrowserContextMenuEnabled = false '禁止右击时显示快捷菜单(同ie中右键菜单)webBrowser1.WebBrowserShortcutsEnabled = false '禁止控件响应快捷键webBrowser1.ScriptErroRSSuppressed=true'禁止显示脚本代码问题的错误信息。

原文链接:https://www.f2er.com/vb/261622.html

猜你在找的VB相关文章