<System.Runtime.InteropServices.ComVisible(True)> _
Public Class Form1
....
Now that you have done one of the above, you also need to inform the WebBrowser control which object will be handling the DHTML events. This is done by setting the ObjectForScripting property of the WebBrowser to the parent class. Because this property is not available at design time, you need to set it at run time, generally in the Forms Load event....
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.ObjectForScripting = Me
End Sub
To make debugging of your application easier, it is recommended that you do not suppress the WebBrowser's scripting errors. This is done by ensuring the WebBrowsers ScriptErrorsSuppressed property is set to False.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<htm>
<body>
<button onclick="window.external.SimpleCall()">Call an application routine from script</button><br />
<button onclick="window.external.ParameterCall(99, 'some text')">Call an application routine from script, with parameters</button><br />
<br />
<input type="text" value="Some text" id="txtTest"/><br />
<button onclick="window.external.ReferenceCall('txtTest')">Call an application routine from script, sending an elements ID</button><br />
<br />
Reference the calling element<input type="checkbox" Checked="Checked" value="test" onclick="window.external.ReferenceCall2(this)"/>
</body>
</html>
If your HTML is stored in a String variable, you add it to your WebBrowser control like this....
WebBrowser1.DocumentText = HTMLstring
Public Sub SimpleCall()
MsgBox("Simple Call")
End Sub
Public Sub ParameterCall(ByVal i As Integer, ByVal s As String)
MsgBox(String.Format("i = {0}, s = {1}", i, s))
End Sub
Note how each of the routines has a declaration context of "Public". That is equied to make these routines visible to COM.
Public Sub ReferenceCall(ByVal cid As String)
Dim c As HtmlElement = WebBrowser1.Document.GetElementById(cid)
MsgBox(c.GetAttribute("value"))
End Sub
The fourth call ('ReferenceCall2') passes the element which raised the event. It requires special handling because it passes an MSHTML object. This requires adding a reference to your project (to Microsoft.mshtml, which is located under the .Net tab of the Add Reference dialog)
Public Sub ReferenceCall2(ByVal c As Object)
MsgBox(CType(c, mshtml.IHTMLInputElement).checked)
End Sub
<System.Runtime.InteropServices.ComVisible(True)> _
Public Class Form1
Private HTMLstring As String = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">" & _
"<html>" & _
"<body>" & _
"<button onclick=""window.external.SimpleCall()"">Call an application routine from script</button><br />" & _
"<button onclick=""window.external.ParameterCall(99, 'some text')"">Call an application routine from script, with parameters</button><br />" & _
"<br />" & _
"<input type=""text"" value=""Some text"" id=""txtTest""/><br />" & _
"<button onclick=""window.external.ReferenceCall('txtTest')"">Call an application routine from script, sending an elements ID</button><br />" & _
"<br/>" & _
"Reference the calling element<input type=""checkbox"" Checked=""Checked"" onclick=""window.external.ReferenceCall2(this)""/>" & _
"</body>" & _
"</html>"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.ObjectForScripting = Me
WebBrowser1.ScriptErrorsSuppressed = False
WebBrowser1.DocumentText = HTMLstring
End Sub
Public Sub SimpleCall()
MsgBox("Simple Call")
End Sub
Public Sub ParameterCall(ByVal i As Integer, ByVal s As String)
MsgBox(String.Format("i = {0}, s = {1}", i, s))
End Sub
Public Sub ReferenceCall(ByVal cid As String)
Dim c As HtmlElement = WebBrowser1.Document.GetElementById(cid)
MsgBox(c.GetAttribute("value"))
End Sub
Public Sub ReferenceCall2(ByVal c As Object)
MsgBox(CType(c, mshtml.IHTMLInputElement).checked)
End Sub
End Class
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)