Interaction between WebBrowser Control and Windows Forms
You can manipulate the Form
and Controls
or call C# methods from WebBrowser
using JavaScript and also you can manipulate content of WebBrowser
control or call JavaScript methods from C#.
Manipulate WinForms from Html
To manipulate your Form
from WebBrowser
control and call C# methods and access your form properties you should decorate your form with [ComVisibleAttribute(true)]
then you can set the form as ObjectForScripting
property of WebBrowser
control.
[ComVisibleAttribute(true)]
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.ObjectForScripting = this;
}
}
Then you can simply call methods and access to elements of your windows form this way:
Call C# method from JavaScript:
window.external.SomeCSMethod('Method called from JavaScript');
Set value of a WinForms Control from JavaScript:
Make the textBox1
control on your Form
to be public by setting the value of Modifier
property to public
using desginer. Then it can be accessible from JavaScript:
window.external.textBox1.Text='Value set from JavaScript';
Manipulate Html from WinForms
You can manipulate html content of web browser control from C# code and call JavaScript methods or set value of html elements using methods of Document
property of WebBrowser
control:
Call JavaScript method from C#:
this.webBrowser1.Document.InvokeScript("someJSMethod", new []{"Method called from C#"});
Set value of a Html Control from C#:
this.webBrowser1.Document.GetElementById("text1")
.SetAttribute("Value set from C#", "Value From C#");
Sample Code:
You can create a Form1
class and put button1
and button2
and textBox1
and webBrowser1
on your Form
set the Modifer
of textBox1
to public:
[ComVisibleAttribute(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
button1.Click += button1_Click;
button2.Click += button2_Click;
}
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.DocumentText =
@"<html>
<head>
<title>Test</title>
<script>
function someJSMethod(value){alert(value);}
</script>
</head>
<body>
<input type=""text"" id=""text1""/>
<br/>
<input type=""button"" value=""Call C# Method"" id=""button1""
onclick=""window.external.SomeCSMethod('Method called from JavaScript');""/>
<br/>
<input type=""button"" value=""Set WinForms Control Value"" id=""button2""
onclick=""window.external.textBox1.Text='Value set from JavaScript';""/>
</body>
</html>";
this.webBrowser1.ObjectForScripting = this;
}
public void SomeCSMethod(string value)
{
MessageBox.Show(value);
}
private void button1_Click(object sender, EventArgs e)
{
this.webBrowser1.Document
.InvokeScript("someJSMethod", new[]{ "Method called from C#" });
}
private void button2_Click(object sender, EventArgs e)
{
this.webBrowser1.Document.GetElementById("text1")
.SetAttribute("value", "Value set from C#");
}
}
How can I pass data to/from a WebBrowser control?
You can pass and get data by calling methods from passed object as ObjectForScripting.
Try this-
[ComVisible(true)]
public class MyScriptingClass{
private string SomeData;
public string GetSomeData(){
return SomeData + " Something";
}
public void SetSomeData(string some){
SomeData = some;
}
}
And set ObjectForScripting property of your webBrowser control -
webBrowser.ObjectForScripting = new MyScriptingClass();
Now, in your javascript code call the methods like this -
var someVarFromJs = window.external.GetSomeData();
window.external.SetSomeData("Something to set");
WinForm with WebBrowser control pops Windows Security for Authentication
According to this, here is how to solve the problem (for future questionnaires) :
- Open Edge
- Select the "..." and then open to Internet Explorer
- From Internet Explorer go : tools("gear")->Internet Options->Security
- Then select custom level
- From the settings deselect "logon in Intranet" and select "Anonymous logon"
Why that happened :
Probably it detects "saml.company"and considers it as intranet.
Both WebBrowser control and the Edge, seem to inherit setting from Internet Explorer. And that seems to be the reason that causes that.
webbrowser component interaction with webpage
Load the web page with Webbrowser.Navigate()
then get the html of the loaded page using the WebBrowser.DocumentText
property.
You have to read this property after the page is loaded so use it in the DocumentCompleted
handler.
Windows Forms WebBrowser control and iframes
Try using the "frames" collection instead. From MSDN:
The iframe element functions as a
document within a document, or like a
floating frame. The frames collection
provides access to the contents of an
iframe. Use the frames collection to
read or write to elements contained in
an iframe. For example, the syntax for
accessing the backgroundColor style of
the body object in an iframe is:sColor =
document.frames("sFrameName").document.body.style.backgroundColor;
Related Topics
How to Change the Color of Winform Datagridview Header
Entity Framework - Stored Procedure Return Value
Generate Ef Orderby Expression by String
How to Recognize If a String Contains Unicode Chars
Differencebetween .Equals and ==
Active Directory Com Exception - an Operations Error Occurred (0X80072020)
Programmatic Way to Get All the Available Languages (In Satellite Assemblies)
How to Put Text on Progressbar
Sqlexception:String or Binary Data Would Be Truncated
Using Appdomain in C# to Dynamically Load and Unload Dll
Check If a Property Exists in a Class
..The Underlying Connection Was Closed: an Unexpected Error Occurred on a Receive
C# Linq Where Date Between 2 Dates
How to Convert Percentage String to Double
Why Is List When Passed Without Ref to a Function Acting Like Passed with Ref
C# String Replace with Dictionary
Why Is Parallel.Foreach Much Faster Then Asparallel().Forall() Even Though Msdn Suggests Otherwise