[英]Interaction between WebBrowser Control and Windows Forms
I am managing various contact information - phone, address, email, IM, other and I want it to look good and save real estate, so I want to display it in a WebBrowser control. 我正在管理各种联系信息 - 电话,地址,电子邮件,IM,其他我希望它看起来很好并节省空间,所以我想在WebBrowser控件中显示它。 I can create the markup and content dynamically into a stream and display it with any format with colors and font size easily adjusted. 我可以动态地将标记和内容创建到流中,并以任何格式显示它,颜色和字体大小可以轻松调整。 I can also put buttons <input>
for Add, Edit, and Delete. 我还可以将按钮<input>
添加到“添加”,“编辑”和“删除”。 I like this method, because it seems easier and better looking than a RichTextBox (correct me if you think otherwise.) 我喜欢这种方法,因为它看起来比RichTextBox更容易和更好看(如果不这样的话,请更正我。)
The question is about responding to those buttons. 问题是关于响应这些按钮。 If one is selected, I want to hide the WebBrowser and unhide a Panel with the TextBox controls needed to allow entry of a new contact or editing of an existing one. 如果选择了一个,我想隐藏WebBrowser并取消隐藏Panel,其中包含允许输入新联系人或编辑现有联系人所需的TextBox控件。 I've heard this can be done. 我听说这可以做到。 I was hoping for suggestions. 我希望得到一些建议。 All I can think of is some code to pick up an AJAX call, that would raise a Windows event, but that seems... odd. 所有我能想到的是一些代码来获取一个AJAX调用,这会引发一个Windows事件,但这看起来很奇怪。
Any ideas, links or suggestions would be appreciated .. or even a good reason why not to do it, but it seems like a good idea for high quality information presentation and I've generated plenty of html dynamically. 任何想法,链接或建议都会受到赞赏..甚至是一个很好的理由,为什么不这样做,但它似乎是高质量的信息呈现的好主意,我已经动态生成了大量的HTML。
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#. 您可以使用JavaScript操作Form
和Controls
或从WebBrowser
调用C#方法,还可以操作WebBrowser
控件的内容或从C#调用JavaScript方法。
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. 要从WebBrowser
控件操作Form
并调用C#方法并访问表单属性,您应该使用[ComVisibleAttribute(true)]
装饰表单,然后可以将表单设置为WebBrowser
控件的ObjectForScripting
属性。
[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: 从JavaScript调用C#方法:
window.external.SomeCSMethod('Method called from JavaScript');
Set value of a WinForms Control from JavaScript: 从JavaScript设置WinForms控件的值:
Make the textBox1
control on your Form
to be public by setting the value of Modifier
property to public
using desginer. 让textBox1
您的控制Form
是通过的值设置公共Modifier
属性为public
使用desginer。 Then it can be accessible from JavaScript: 然后可以从JavaScript访问它:
window.external.textBox1.Text='Value set from JavaScript';
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: 您可以使用C#代码操作Web浏览器控件的html内容,并使用WebBrowser
控件的Document
属性方法调用JavaScript方法或设置html元素的值:
Call JavaScript method from C#: 从C#调用JavaScript方法:
this.webBrowser1.Document.InvokeScript("someJSMethod", new []{"Method called from C#"});
Set value of a Html Control from C#: 从C#设置Html控件的值:
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: 您可以创建一个Form1
类,并将button1
和button2
以及textBox1
和webBrowser1
放在Form
将textBox1
的Modifer
设置为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#");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.