Update webbrowser sample (#173)

* Update webbrowser sample

As per suggestions https://github.com/dotnet/winforms/issues/4443#issuecomment-759333446

Resolves dotnet/winforms#4443

* fixup! Update webbrowser sample

* fixup! Update webbrowser sample

* Update dotnet-desktop-guide/framework/winforms/controls/implement-two-way-com-between-dhtml-and-client.md

Co-authored-by: Andy De George <[email protected]>

Co-authored-by: Andy De George <[email protected]>
This commit is contained in:
Igor Velikorossov
2021-01-27 14:21:32 -08:00
committed by GitHub
co-authored by Andy De George
parent 232f1b91e2
commit 1fe35d6424
5 changed files with 62 additions and 32 deletions
@@ -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();
@@ -25,11 +22,12 @@ public class Form1 : Form
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>
@@ -40,7 +38,7 @@ public class Form1 : Form
webBrowser1.WebBrowserShortcutsEnabled = false;
//</snippet3>
//<snippet4>
webBrowser1.ObjectForScripting = this;
webBrowser1.ObjectForScripting = new MyScriptObject(this);
//</snippet4>
//<snippet9>
// Uncomment the following line when you are finished debugging.
@@ -56,13 +54,6 @@ public class Form1 : Form
"</body></html>";
}
//<snippet5>
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
//</snippet5>
private void button1_Click(object sender, EventArgs e)
{
//<snippet8>
@@ -71,4 +62,21 @@ public class Form1 : Form
//</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>
@@ -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>