微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/vb/261622.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐