Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,14 @@ The <xref:System.Windows.Forms.WebBrowser> control lets you implement two-way co
[!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#3](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#3)]
[!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#3](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#3)]

4. Set the <xref:System.Windows.Forms.WebBrowser.ObjectForScripting%2A> property in the form's constructor or a <xref:System.Windows.Forms.Form.Load> event handler.
4. Set the <xref:System.Windows.Forms.WebBrowser.ObjectForScripting%2A> property in the form's constructor or override the <xref:System.Windows.Forms.Form.OnLoad%2A> method.

The following code uses the form class itself for the scripting object.

> [!NOTE]
> Component Object Model (COM) must be able to access the scripting object. To make your form visible to COM, add the <xref:System.Runtime.InteropServices.ComVisibleAttribute> attribute to your form class.

[!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#4](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#4)]
[!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#4](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#4)]

5. Implement public properties or methods in your application code that your script code will use.

For example, if you use the form class for the scripting object, add the following code to your form class.
5. Implement your scripting object.

[!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#5](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#5)]
[!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#5](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#5)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//<snippet0>
using System;
using System.Windows.Forms;
using System.Security.Permissions;

[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Form1 : Form
{
private WebBrowser webBrowser1 = new WebBrowser();
Expand All @@ -25,11 +22,12 @@ public Form1()
webBrowser1.Dock = DockStyle.Fill;
Controls.Add(webBrowser1);
Controls.Add(button1);
Load += new EventHandler(Form1_Load);
}

private void Form1_Load(object sender, EventArgs e)
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

//<snippet1>
webBrowser1.AllowWebBrowserDrop = false;
//</snippet1>
Expand All @@ -40,7 +38,7 @@ private void Form1_Load(object sender, EventArgs e)
webBrowser1.WebBrowserShortcutsEnabled = false;
//</snippet3>
//<snippet4>
webBrowser1.ObjectForScripting = this;
webBrowser1.ObjectForScripting = new MyScriptObject(this);
//</snippet4>
//<snippet9>
// Uncomment the following line when you are finished debugging.
Expand All @@ -56,13 +54,6 @@ private void Form1_Load(object sender, EventArgs e)
"</body></html>";
}

//<snippet5>
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
//</snippet5>

private void button1_Click(object sender, EventArgs e)
{
//<snippet8>
Expand All @@ -71,4 +62,21 @@ private void button1_Click(object sender, EventArgs e)
//</snippet8>
}
}

//<snippet5>
public class MyScriptObject
{
private Form1 _form;

public MyScriptObject(Form1 form)
{
_form = form;
}

public void Test(string message)
{
MessageBox.Show(message, "client code");
}
}
//</snippet5>
//</snippet0>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net472;netcoreapp3.1;net5.0-windows</TargetFrameworks>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'<snippet0>
Imports System.Windows.Forms
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
Inherits Form

Expand All @@ -24,8 +21,8 @@ Public Class Form1
Controls.Add(button1)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles Me.Load
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)

'<snippet1>
webBrowser1.AllowWebBrowserDrop = False
Expand All @@ -37,7 +34,7 @@ Public Class Form1
webBrowser1.WebBrowserShortcutsEnabled = False
'</snippet3>
'<snippet4>
webBrowser1.ObjectForScripting = Me
webBrowser1.ObjectForScripting = New MyScriptObject(Me)
'</snippet4>
'<snippet9>
' Uncomment the following line when you are finished debugging.
Expand All @@ -53,12 +50,6 @@ Public Class Form1
"</body></html>"
End Sub

'<snippet5>
Public Sub Test(ByVal message As String)
MessageBox.Show(message, "client code")
End Sub
'</snippet5>

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles button1.Click

Expand All @@ -70,4 +61,19 @@ Public Class Form1
End Sub

End Class

'<snippet5>
Public Class MyScriptObject
Private _form As Form1

Public Sub New(ByVal form As Form1)
_form = form
End Sub

Public Sub Test(ByVal message As String)
MessageBox.Show(message, "client code")
End Sub

End Class
'</snippet5>
'</snippet0>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net472;net5.0-windows</TargetFrameworks>
<RootNamespace>sample</RootNamespace>
<UseWindowsForms>true</UseWindowsForms>
<MyType>WindowsForms</MyType>
<StartupObject>sample.Form1</StartupObject>
</PropertyGroup>

</Project>